📄 proxy4.py
字号:
#!/usr/bin/env python# Fourth incarnation of Amit's web proxy.from proxy4_base import *import proxy4_basefrom transport.listener import Listenerimport transport.dns_lookupsimport transport.http_serverimport transport.http_clientimport proxy4_interp# TODO: investigate using TCP_NODELAY (disable Nagle)def periodic_print_socketlist(): print 'connections: [', for conn in asyncore.socket_map.values(): if conn is asyncore.socket_map.values()[-1]: # It's the last one, so don't print a comma or indentation print conn, else: print '%s' % conn print ' ', print ']' #print 'dnscache:', proxy4_dns.DnsCache make_timer(60, periodic_print_socketlist)def proxy_poll(timeout=0.0): smap = asyncore.socket_map if smap: r = filter(lambda x: x.readable(), smap.values()) w = filter(lambda x: x.writable(), smap.values()) e = smap.values() (r,w,e) = select.select(r,w,e, timeout) # Make sure we only process one type of event at a time, # because if something needs to close the connection we # don't want to call another handle_* on it handlerCount = 0 for x in e: try: x.handle_expt_event() handlerCount = handlerCount + 1 except: x.handle_error(sys.exc_type, sys.exc_value, sys.exc_traceback) for x in w: try: t = time.time() if x not in e: x.handle_write_event() handlerCount = handlerCount + 1 if time.time() - t > 0.1: message(5, 'wslow', '%4.1f' % (time.time() - t), 's', x) except: x.handle_error(sys.exc_type, sys.exc_value, sys.exc_traceback) for x in r: try: t = time.time() if x not in e and x not in w: x.handle_read_event() handlerCount = handlerCount + 1 if time.time() - t > 0.1: message(5, 'rslow', '%4.1f' % (time.time() - t), 's', x) except: x.handle_error(sys.exc_type, sys.exc_value, sys.exc_traceback) return handlerCount def mainloop(): # I wrap these in a lambda/apply so that if the module is # reloaded, I can use the NEW classes Listener(4444, lambda *args: apply(transport.http_client.HttpClient, args)) Listener(4445, lambda *args: apply(proxy4_interp.InterpreterConnection, args)) # make_timer(5, transport.http_server.speedcheck_print_status) make_timer(60, periodic_print_socketlist) while 1: timeout = max(0, run_timers()) # Installing a timeout means we're in a handler, and after # dealing with handlers, we come to the main loop, so we don't # have to worry about being in asyncore.poll when a timer goes # off. handlerCount = proxy_poll(timeout=timeout)if __name__=='__main__': mainloop()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -