1 Star 0 Fork 46

cutecuteyu/qytang_Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
2016.1.17 流量重定向 test_socket_stream_redirect.py 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
现任明教教主-乾颐堂 提交于 2016-01-17 11:43 . new file
import sys, os, multiprocessing
from socket_stream_redirect import *
def server1():
mypid = os.getpid()
conn = initListenerSocket()
file = conn.makefile('r')
for i in range(3):
data = file.readline().rstrip()
print('server %s get [%s]' % (mypid, data))
def client1():
mypid = os.getpid()
redirectOut()
for i in range(3):
print('client %s: %s' % (mypid, i))
sys.stdout.flush()
##############################################################
def server2():
mypid = os.getpid()
conn = initListenerSocket()
for i in range(3):
conn.send(('server %s: %s\n' % (mypid, i)).encode())
def client2():
mypid = os.getpid()
redirectIn()
for i in range(3):
data = input()
print('client %s get [%s]' % (mypid, data))
##############################################################
def server3():
mypid = os.getpid()
conn = initListenerSocket()
file = conn.makefile('r')
for i in range(3):
data = file.readline().rstrip()
conn.send(('server %s got [%s]\n' % (mypid, data)).encode())
def client3():
mypid = os.getpid()
redirectBothAsClient()
for i in range(3):
print('client %s: %s' % (mypid, i))
data = input()
sys.stderr.write('client %s got [%s]\n' % (mypid, data))
##############################################################
def server4():
mypid = os.getpid()
sock = socket(AF_INET, SOCK_STREAM)
sock.connect((host, port))
file = sock.makefile('r')
for i in range(3):
sock.send(('server %s: %s\n' % (mypid, i)).encode())
data = file.readline().rstrip()
print('server %s got [%s]' % (mypid, data))
def client4():
mypid = os.getpid()
redirectBothAsServer()
for i in range(3):
data = input()
print('client %s got [%s]' % (mypid, data))
sys.stdout.flush()
##############################################################
def server5():
mypid = os.getpid()
conn = initListenerSocket()
file = conn.makefile('r')
for i in range(3):
conn.send(('server %s: %s\n' % (mypid, i)).encode())
data = file.readline().rstrip()
print('server %s got [%s]' % (mypid, data))
def client5():
mypid = os.getpid()
redirectBothAsClient()
for i in range(3):
data = input()
print('client %s got [%s]\n' % (mypid, data))
sys.stdout.flush()
if __name__ == '__main__':
server = eval('server' + sys.argv[1])
client = eval('client' + sys.argv[1])
multiprocessing.Process(target = server).start()
client()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/cutecuteyu/qytang_Python.git
git@gitee.com:cutecuteyu/qytang_Python.git
cutecuteyu
qytang_Python
qytang_Python
master

搜索帮助