This article describes the Python implementation of port reuse as shown in the following example:
#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
Example 2
Import socket
TCP1 = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
tcp2 = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
#在绑定前调用setsockopt让套接字允许地址重用
tcp1.setsockopt (socket. Sol_socket, SOCKET. So_reuseport, 1)
tcp2.setsockopt (socket. Sol_socket, SOCKET. So_reuseport, 1)
#接下来两个套接字都也可以绑定到同一个端口上 tcp1.bind (('
0.0.0.0 ', 12345))
Tcp2.bind ((' 0.0.0.0 ', 12345))
Import Socket
TCP1 = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
tcp2 = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
#在绑定前调用setsockopt让套接字允许地址重用
tcp1.setsockopt (socket. Sol_socket, SOCKET. So_reuseport, 1)
tcp2.setsockopt (socket. Sol_socket, SOCKET. So_reuseport, 1)
#接下来两个套接字都也可以绑定到同一个端口上
tcp1.bind ((' 0.0.0.0 ', 12345))
Tcp2.bind ((' 0.0.0.0 ', 12345) )
Example 3
redirect
import socket,os Buflen = 4*1024 sock1 = socket.socket (socket.af_inet, socket. Sock_stream) Sock1.bind ((' 192.168.168.100 ', 8000)) Sock1.listen (5) Sock2 = Socket.socket (socket.af_inet, socket. SOCK_STREAM) sock2.connect (' 192.168.168.100 ', 12345) while true:connection,address = Sock1.accept () BUF = Co NNECTION.RECV (Buflen) #print buf sock2.send (buf) connection.send (SOCK2.RECV (buflen)) connection.cl OSE () import Socket,os Buflen = 4*1024 sock1 = socket.socket (socket.af_inet, socket. Sock_stream) Sock1.bind ((' 192.168.168.100 ', 8000)) Sock1.listen (5) Sock2 = Socket.socket (socket.af_inet, socket. SOCK_STREAM) sock2.connect (' 192.168.168.100 ', 12345) while true:connection,address = Sock1.accept () BUF = Co NNECTION.RECV (Buflen) #print buf sock2.send (buf) connection.send (SOCK2.RECV (buflen)) connection.cl OSE ()