python实现超简单端口转发的方法

yipeiwu_com6年前Python基础

本文实例讲述了python实现超简单端口转发的方法。分享给大家供大家参考。具体如下:

代码非常简单,实现了简单的端口数据转发功能,用于真实环境还需要再修改一下。

复制代码 代码如下:
#tcp server
import socket
host = '127.0.0.1'          #Local Server IP
host2 = '127.0.0.1'   #Real Server IP
port = 6001 #Local Server Port
port2 = 7001 #Real Server Port
def ProcData(data):
    return data
    #add more code....
print "Map Server start from " + host + ":" + str(port) +" to " + host2 + ":" + str(port2) +"\r\n"
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('127.0.0.1',port))
print "127.0.0.1 Server start at "+ str(port) +"\r\n"
client = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
client.connect((host2,port2))
print host +" Client connect to " + host2 + ":"+str(port2)+"\n"
server.listen(5)
ss, addr = server.accept()
print 'got connected from',addr
while 1:
    msg = ss.recv(20480)
    print "Get:"+repr(msg)+"\r\n"
    client.send(msg)
    #print "Client send data %s to "%repr(msg)
    buf=client.recv(20480)
    #print "Client recv data %s from "%repr(buf)
    ss.send(buf)
    print "Send:"+repr(buf)+"\r\n"

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python三方库之requests的快速上手

本文基于2.21.0 发送请求 发送GET请求: r = requests.get('https://api.github.com/events') 发送POST请求: r...

Python利用ElementTree模块处理XML的方法详解

前言 最近因为工作的需要,在使用 Python 来发送 SOAP 请求以测试 Web Service 的性能,由于 SOAP 是基于 XML 的,故免不了需要使用 python 来处理...

基于python中__add__函数的用法

运算符重载 _add ##定义:让自定义的类生成的对象(实例)能够使用运算符进行操作 class Vector01: ‘'‘定义一个一维向量''' def init(self,x...

python安装本地whl的实例步骤

python安装本地whl的实例步骤

1.用管理员打开cmd 2.首先通过pip命令安装wheel pip install wheel 如果提示'pip'不是内部或外部命令,也不是可运行的程序或批处理文件 ①将pytho...

python conda操作方法

conda 虚拟环境安装 List item conda env list #查看已安装虚拟环境 coda创建虚拟环境非常方便:官方教程:https://conda.io/project...