1 Star 0 Fork 46

zhaoshucai/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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/zhaoshucai/qytang_Python.git
git@gitee.com:zhaoshucai/qytang_Python.git
zhaoshucai
qytang_Python
qytang_Python
master

搜索帮助