Recently I plan to study the socket programming in python in depth, so I plan to study it. I copied it and found it is really not easy to write it. there are many problems in the middle, but it is really easy to see, it's hard to do it. recently I plan to study python's socket programming in depth. so I plan to take a look at it. I found it is really not easy to write it. there are many problems in the middle, but it is really easy to see, difficult to do
import socketimport threadimport urlparseimport select BUFLEN=8192 class Proxy(object): def __init__(self,conn,addr): self.source=conn self.request="" self.headers={} self.destnation=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.run() def get_headers(self): header='' while True: header+=self.source.recv(BUFLEN) index=header.find('\n') if index >0: break #firstLine,self.request=header.split('\r\n',1) firstLine=header[:index] self.request=header[index+1:] self.headers['method'],self.headers['path'],self.headers['protocol']=firstLine.split() def conn_destnation(self): url=urlparse.urlparse(self.headers['path']) hostname=url[1] port="80" if hostname.find(':') >0: addr,port=hostname.split(':') else: addr=hostname port=int(port) ip=socket.gethostbyname(addr) print ip,port self.destnation.connect((ip,port)) data="%s %s %s\r\n" %(self.headers['method'],self.headers['path'],self.headers['protocol']) self.destnation.send(data+self.request) print data+self.request def renderto(self): readsocket=[self.destnation] while True: data='' (rlist,wlist,elist)=select.select(readsocket,[],[],3) if rlist: data=rlist[0].recv(BUFLEN) if len(data)>0: self.source.send(data) else: break def run(self): self.get_headers() self.conn_destnation() self.renderto() class Server(object): def __init__(self,host,port,handler=Proxy): self.host=host self.port=port self.server=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server.bind((host,port)) self.server.listen(5) self.handler=handler def start(self): while True: try: conn,addr=self.server.accept() thread.start_new_thread(self.handler,(conn,addr)) except: pass if __name__=='__main__': s=Server('127.0.0.1',8080) s.start()import socketimport threadimport urlparseimport selectBUFLEN=8192class Proxy(object): def __init__(self,conn,addr): self.source=conn self.request="" self.headers={} self.destnation=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.run() def get_headers(self): header='' while True: header+=self.source.recv(BUFLEN) index=header.find('\n') if index >0: break #firstLine,self.request=header.split('\r\n',1) firstLine=header[:index] self.request=header[index+1:] self.headers['method'],self.headers['path'],self.headers['protocol']=firstLine.split() def conn_destnation(self): url=urlparse.urlparse(self.headers['path']) hostname=url[1] port="80" if hostname.find(':') >0: addr,port=hostname.split(':') else: addr=hostname port=int(port) ip=socket.gethostbyname(addr) print ip,port self.destnation.connect((ip,port)) data="%s %s %s\r\n" %(self.headers['method'],self.headers['path'],self.headers['protocol']) self.destnation.send(data+self.request) print data+self.request def renderto(self): readsocket=[self.destnation] while True: data='' (rlist,wlist,elist)=select.select(readsocket,[],[],3) if rlist: data=rlist[0].recv(BUFLEN) if len(data)>0: self.source.send(data) else: break def run(self): self.get_headers() self.conn_destnation() self.renderto() class Server(object): def __init__(self,host,port,handler=Proxy): self.host=host self.port=port self.server=socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server.bind((host,port)) self.server.listen(5) self.handler=handler def start(self): while True: try: conn,addr=self.server.accept() thread.start_new_thread(self.handler,(conn,addr)) except: passif __name__=='__main__': s=Server('127.0.0.1',8080) s.start()
In fact, the Http proxy server itself is not difficult, but it still takes a lot of trouble to write it out. here we will not elaborate on the source code, it is very simple. I have encountered some problems.
I: I only knew that thread. the first parameter of start_new_thread is a function object. but when I saw the previous blog post, I felt a sigh of relief. this is also acceptable, so I quickly tested it:
import thread class Hello: def __init__(self,content): print content def cs(): thread.start_new_thread(Hello, ("Hello World",)) if __name__=='__main__': cs()import threadclass Hello: def __init__(self,content): print contentdef cs(): thread.start_new_thread(Hello, ("Hello World",))if __name__=='__main__': cs()
Unhandled exception in thread started
Error in sys. mongothook:
Original exception was:
Unhandled exception in thread started
Error in sys. mongothook:
Original exception was:
A look, I said, how can the first parameter be an object? I smiled and despised the author a little. So I went to bed, and the next day, I still don't give up, so I went down the code and tried it locally. Yes, I realized I was 2, so I immediately Baidu.
In the original thread module, if the main thread ends before the sub-thread, this exception will be thrown. Therefore, we must end the sub-thread first, the simplest way is to let the main thread sleep for a long enough time. as to how long it will take, it seems that I don't know. how can this problem be solved?
A better solution is that the main thread adds a lock to each sub-thread, the sub-thread releases the lock before the end, and the main thread continuously checks the lock status. The code is as follows:
import thread class Hello: def __init__(self,content,lock): print content """ do something .... At the end,release the lock """ lock.release() def cs(): lock=thread.allocate_lock() lock.acquire() thread.start_new_thread(Hello, ("Hello World",lock)) while True: if not lock.locked(): break print "lock release" if __name__=='__main__': cs()import threadclass Hello: def __init__(self,content,lock): print content """ do something .... At the end,release the lock """ lock.release()def cs(): lock=thread.allocate_lock() lock.acquire() thread.start_new_thread(Hello, ("Hello World",lock)) while True: if not lock.locked(): break print "lock release"if __name__=='__main__': cs()
2. the second error is compared to 2.
Self. source. send [data]
PeError: 'builtin _ function_or_method 'object is unsubscriptable
Self. source. send [data]
TypeError: 'builtin _ function_or_method 'object is unsubscriptable
The main meaning is that built-in functions or methods cannot have subscripts, you know