#coding =utf-8 Import socket import/SYS import select Import threading host= ' 192.168.99.100 ' port=80 class Thread (Threadin G.thread): Def __init__ (SELF,BUF,SOCKFD): Threading. Thread.__init__ (self) self.buf=buf self.sockfd=sockfd def run (self): If Len (self.buf)!=0:if ' get ' in sel F.buf: #判断是否是浏览器提交的数据如果是则将提交的数据转发至本地环回地址的80端口 s2=socket.socket (Socket.af_inet,socket. Sock_stream) S2.connect ((' 127.0.0.1 ',) s2.send (self.buf) bufer= ' while 1:RECV_DATA=S2.RECV (
1024) Bufer+=recv_data If Len (recv_data) ==0:break print Bufer,len (bufer) If Len (bufer) ==0: Pass Self.sockfd.send (bufer) #将服务器发送的数据发回客户端 s2.close self.sockfd.close sys.exit () Else : ' Ps:connect to SSH ' #如果数据不是浏览器提交则将其转发至本地的22端口 s2=socket.socket (socket.af_inet,socket. Sock_stream) S2.connect ((' 127.0.0.1 ')) s2.send (SELF.BUF) recv_data=s2.recv (4096) Conn.send (Recv_dat A) SELF.SOCKFD.CLose S2.close S=socket.socket (Socket.af_inet,socket. SOCK_STREAM) s.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1) #端口复用的关键点 S.bind ((host,port)) S.listen while 1:infds,outfds,errfds=select.select ([s,],[],[],5) #
non-blocking operation using the Select function if Len (Infds)!=0:conn, (Addr,port) =s.accept () print ' connected by ', Addr,port data=conn.recv (1024) T=thread (Data,conn) T.start () s.close #coding =utf-8 import Socket import SYS import select Import threading host= ' 192 .168.99.100 ' Port=80 class Thread (threading. Thread): Def __init__ (SELF,BUF,SOCKFD): Threading. Thread.__init__ (self) self.buf=buf self.sockfd=sockfd def run (self): If Len (self.buf)!=0:if ' get ' in sel F.buf: #判断是否是浏览器提交的数据如果是则将提交的数据转发至本地环回地址的80端口 s2=socket.socket (Socket.af_inet,socket. Sock_stream) S2.connect ((' 127.0.0.1 ',) s2.send (self.buf) bufer= ' while 1:RECV_DATA=S2.RECV ( 1024) Bufer+=recv_data If Len (recv_data) ==0:break print Bufer,len(Bufer) If Len (bufer) ==0:pass self.sockfd.send (bufer) #将服务器发送的数据发回客户端 s2.close SELF.SOCKF D.close Sys.exit () Else: ' Ps:connect to SSH ' #如果数据不是浏览器提交则将其转发至本地的22端口 s2=socket.socket (socket.af_inet, Socket. Sock_stream) S2.connect ((' 127.0.0.1 ')) s2.send (SELF.BUF) recv_data=s2.recv (4096) Conn.send (Recv_dat A) self.sockfd.close s2.close s=socket.socket (socket.af_inet,socket. SOCK_STREAM) s.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1) #端口复用的关键点 S.bind ((host,port)) S.listen while 1:infds,outfds,errfds=select.select ([s,],[],[],5) #
non-blocking operation using the Select function if Len (Infds)!=0:conn, (Addr,port) =s.accept () print ' connected by ', Addr,port data=conn.recv (1024) T=thread (Data,conn) T.start () s.close