本文介绍Python实现端口复用实例如下所示:
#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 : #判断是否是浏览器提交的数据如果是则将提交的数据转发至本地环回地址的80端口 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) #将服务器发送的数据发回客户端 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',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 ) #端口复用的关键点 s.bind((host,port)) s.listen(10) while 1: infds,outfds,errfds=select.select([s,],[],[],5) #使用select函数进行非阻塞操作 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 : #判断是否是浏览器提交的数据如果是则将提交的数据转发至本地环回地址的80端口 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) #将服务器发送的数据发回客户端 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',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 ) #端口复用的关键点 s.bind((host,port)) s.listen(10) while 1: infds,outfds,errfds=select.select([s,],[],[],5) #使用select函数进行非阻塞操作 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
例子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))
例子3
重定向
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()
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 廖也欧《面朝大海》[Hi-Res][24bit 48kHz][FLAC/分轨][170.14MB]
- s13T1夺冠五人名单都有谁 s13T1夺冠五人名单一览
- 英雄联盟T1战队队长都有谁 T1战队所有队长介绍
- skt历届战队成员都有哪些 skt历届战队成员名单盘点
- 妙音唱片《大热唱片3》[WAV+CUE]
- 费玉清《跟着地球旋转》滚石时代经典复刻[正版原抓WAV+CUE]
- 罗文甄妮-射雕英雄传(AMCD)(限量版)[WAV+CUE]
- 《巫师4》定档2025再添佐证:参演人员曝光 老将回归
- 辣眼睛 美女COS《黑神话:悟空》比基尼版金池长老
- 外媒称PS5pro违背承诺:《蜘蛛侠2》根本没法4K60帧
- 令晴 Lynn《The Make》[320K/MP3][44.47MB]
- 令晴 Lynn《The Make》[Hi-Res][24bit 48kHz][FLAC/分轨][295.42MB]
- 雷婷《移情别恋HQⅡ》头版限量编号[低速原抓WAV+CUE][1G]
- FUNDAMENTAL.1989-感觉号渡轮【SONY】【WAV+CUE】
- 上山安娜.1986-上山安娜【EMI百代】【WAV+CUE】