16 Star 44 Fork 45

现任明教教主-乾颐堂 / qytang_Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
2016.1.17 流量重定向 socket_stream_redirect.py 1.26 KB
一键复制 编辑 原始数据 按行查看 历史
现任明教教主-乾颐堂 提交于 2016-01-17 11:42 . new file
import sys
from socket import *
port = 50008
host = 'localhost'
def initListenerSocket(port = port):
sock = socket(AF_INET, SOCK_STREAM)
# sock.setblocking(0)
# sock.settimeout(2)
sock.bind(('', port))
sock.listen(5)
conn, addr = sock.accept()
return conn
def redirectOut(port = port, host = host):
sock = socket(AF_INET, SOCK_STREAM)
# sock.setblocking(0)
# sock.settimeout(2)
sock.connect((host, port))
file = sock.makefile('w')
sys.stdout = file
return sock
def redirectIn(port = port, host = host):
sock = socket(AF_INET, SOCK_STREAM)
# sock.setblocking(0)
# sock.settimeout(2)
sock.connect((host, port))
file = sock.makefile('r')
sys.stdin = file
return sock
def redirectBothAsClient(port = port, host = host):
sock = socket(AF_INET, SOCK_STREAM)
# sock.setblocking(0)
# sock.settimeout(2)
sock.connect((host, port))
ofile = sock.makefile('w')
ifile = sock.makefile('r')
sys.stdout = ofile
sys.stdin = ifile
return sock
def redirectBothAsServer(port = port, host = host):
sock = socket(AF_INET, SOCK_STREAM)
# sock.settimeout(2)
sock.bind((host, port))
sock.listen(5)
conn, addr = sock.accept()
ofile = sock.makefile('w')
ifile = sock.makefile('r')
sys.stdout = ofile
sys.stdin = ifile
return conn
Python
1
https://gitee.com/qytang/qytang_Python.git
git@gitee.com:qytang/qytang_Python.git
qytang
qytang_Python
qytang_Python
master

搜索帮助