Python implements port multiplexing instance code

  • 2020-04-02 13:48:47
  • OfStack

This article introduces an example of Python implementing port reuse as follows:


#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 self.buf :  # Determines if the data is submitted by the browser and if so, forwards the submitted data to the local loopback address 80 port 
     s2=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s2.connect(('127.0.0.1',80))
     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)  # Sends the data sent by the server back to the client 
     s2.close
     self.sockfd.close
     sys.exit()
    else:
     'ps:connect to ssh' # If the data is not submitted by the browser, it is forwarded locally 22 port 
     s2=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s2.connect(('127.0.0.1',22))
     s2.send(self.buf)
     recv_data=s2.recv(4096)
     conn.send(recv_data)
     self.sockfd.close
     s2.close
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) # Key points for port reuse 
s.bind((host,port))
s.listen(10)
while 1:
 infds,outfds,errfds=select.select([s,],[],[],5) # use select The function performs a non-blocking operation 
 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 self.buf :  # Determines if the data is submitted by the browser and if so, forwards the submitted data to the local loopback address 80 port 
     s2=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s2.connect(('127.0.0.1',80))
     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)  # Sends the data sent by the server back to the client 
     s2.close
     self.sockfd.close
     sys.exit()
    else:
     'ps:connect to ssh' # If the data is not submitted by the browser, it is forwarded locally 22 port 
     s2=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     s2.connect(('127.0.0.1',22))
     s2.send(self.buf)
     recv_data=s2.recv(4096)
     conn.send(recv_data)
     self.sockfd.close
     s2.close
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 ) # Key points for port reuse 
s.bind((host,port))
s.listen(10)
while 1:
 infds,outfds,errfds=select.select([s,],[],[],5) # use select The function performs a non-blocking operation 
 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)
# Called before binding setsockopt Let the socket allow the address to be reused 
tcp1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
tcp2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
 # The next two sockets can also be bound to the same port 
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)
# Called before binding setsockopt Let the socket allow the address to be reused 
tcp1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
tcp2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
 # The next two sockets can also be bound to the same port 
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 = connection.recv(bufLen) 
    #print buf      
    sock2.send(buf) 
    connection.send(sock2.recv(bufLen))
    connection.close()
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 = connection.recv(bufLen) 
    #print buf      
    sock2.send(buf) 
    connection.send(sock2.recv(bufLen))
    connection.close()

Related articles: