python套接字流重定向实例汇总
作者:hebedich 发布时间:2022-04-15 07:53:41
标签:python,套接字
将套接字流重定向到标准输入或输出流
#!/usr/bin/env python3
"""
测试socket-stream 重定向模式
"""
import sys,os,time
from multiprocessing import Process
from socket import *
def initListenerSocket(port=50008,host=''):
"""
初始化在服务器模式下调用者用于监听连接的套接字
"""
sock=socket()
try:
sock.bind((host,port))
except OSError as e:
print('Address already in use')
os._exit(1)
sock.listen(5)
conn,addr=sock.accept()
return conn
def redirecOut(port=50008,host='localhost'):
"""
在接受之前其他连接都失败,连接调用者标准输出流
到一个套接字,这个套接字用于gui监听,在收听者启动后,启动调用者
"""
sock=socket()
try:
sock.connect((host,port))
except ConnectionRefusedError as e:
print('connection refuse')
os._exit(1)
file=sock.makefile('w')
sys.stdout=file
return sock
def redirecIn(port=50008,host='localhost'):
"""
连接调用者标准输入流到用于gui来提供的套接字
"""
sock=socket()
try:
sock.connect((host,port))
except ConnectionRefusedError as e:
print('conenction refuse')
os._exit(1)
file=sock.makefile('r')
sys.stdin=file
return sock
def redirecBothAsClient(port=50008,host='localhost'):
"""
在这种模式下,连接调用者标准输入和输出流到相同的套接字
调用者对于服务器来说就是客户端:发送消息,接受响应答复
"""
sock=socket()
try:
sock.connect((host,port))
except ConnectionRefusedError as e:
print('connection refuse')
os._exit(1)
ofile=sock.makefile('w')
ifile=sock.makefile('r')
sys.stdout=ofile
sys.stdin=ifile
return sock
def redirecBothAsServer(port=50008,host='localhost'):
"""
在这种模式下,连接调用者标准输入和输出流到相同的套接字,调用者对于
服务器来说就是服务端:接受消息,发送响应答复
"""
sock=socket()
try:
sock.bind((host,port))
except OSError as e:
print('Address already in use')
os._exit(1)
sock.listen(5)
conn,addr=sock.accept()
ofile=conn.makefile('w')
ifile=conn.makefile('r')
sys.stdout=ofile
sys.stdin=ifile
return conn
def server1():
mypid=os.getpid()
conn=initListenerSocket()
file=conn.makefile('r')
for i in range(3):
data=file.readline().rstrip()
print('server %s got [%s]' %(mypid,data))
def client1():
time.sleep(1)
mypid=os.getpid()
redirecOut()
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 got [%s]\n' %(mypid,i)).encode())
def client2():
time.sleep(1)
mypid=os.getpid()
redirecIn()
for i in range(3):
data=input()
print('client %s got [%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():
time.sleep(1)
mypid=os.getpid()
redirecBothAsClient()
for i in range(3):
print('Client %s: %s' %(mypid,data))
data=input()
sys.stderr.write('client %s got [%s]\n' %(mypid,data))
def server4(port=50008,host='localhost'):
mypid=os.getpid()
sock=socket()
try:
sock.connect((host,port))
ConnectionRefusedError as e:
print('connection refuse')
os._exit(1)
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():
time.sleep(1)
mypid=os.getpid()
redirecBothAsServer()
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()
s=redirecBothAsClient()
for i in range(3):
data=input()
print('client %s got [%s]'%(mypid,data))
sys.stdout.flush()
def main():
server=eval('server'+sys.argv[1])
client=eval('client'+sys.argv[1])
Process(target=server).start()
client()
if __name__=='__main__':
main()


猜你喜欢
- 一、基于PaddleSpeech的婴儿啼哭识别1.项目背景对婴儿来说,啼哭声是一种通讯的方式,一个非常有限的,但类似成年人进行交流的方式。它
- goproxyGo HTTP(S)代理库, 支持中间人代理解密HTTPS项目地址安装go get github.com/ouqiang/go
- 一 Process对象的join方法在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情
- 我们知道了钢琴键盘的音高是其实是有规律的,如下频率翻倍,高一个八度国际基准音:440Hz,钢琴键盘上对应小字一组的la小字一组的la可以看下
- iframe是非常常用的一个html元素,如果在父页面中使用子页面的方法应该怎么写呢,下面就做一下简单的介绍。一、父页面代码<html
- 定时关机,功能:windows下,用户按照一定格式输入关机时间,系统到指定时间自动关闭 思路:从用户输入获取指定时间 分别以时分秒减去当前时
- 要使用request对象的ServerVariables属性,通过它来获得环境变量的值。使用的语法为:Request.ServerVaria
- 以连续3天为例,使用工具:MySQL。1.创建SQL表:create table if not exists orde(id varchar
- pip使用过程中的痛苦,大家相必都已经知道了,目前豆瓣提供了国内的pypi源,源包相对会略有延迟,但不影响基本使用。pip install
- class和id的命名,如果合理,可以使得文档具有清晰的结构我们现在解决办法就是使用现有的元素,通过给他们id或class而得到额外的信息。
- 1.类方法类方法是从属于"类对象"的方法。类对象可以通过装饰器@classmethod来定义,具体格式如下:@class
- 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。生产环境
- 网上关于PyQt5的教程很少,特别是界面跳转这一块儿,自己研究了半天,下来和大家分享一下一、首先是主界面# -*- coding: utf-
- 本文实例讲述了php mysql procedure实现获取多个结果集的方法。分享给大家供大家参考,具体如下:protected funct
- 最近开始学习Python开发,“工欲善其事必先利其器”,Python程序都是用什么工具开发出来的呢。
- 如何限制重复订阅邮件或投票?一、准备子程序和函数。1、初始化数据,在Session对象中保存两个变量:Sub InitializeFID()
- 先说结论1. oracle: oracle 默认没有排序规则2. mysql2.1 innoDB引擎: 默认查询按照id正序排序2.2 my
- 需提前安装好pyzbar和opencv-python库(博主的电脑安装opencv-python库比较麻烦,但大部分都不会出现该问题)安装方
- Sublime Text 3纯文本编辑器Package Control(Sublime的包管理器)Sublime Text3中的插件,通过该
- 本文实例讲述了Python自定义线程池实现方法。分享给大家供大家参考,具体如下:关于python的多线程,由与GIL的存在被广大群主所诟病,