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()

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

在 Sublime Text 中运行 Python 代码,需先安装 Python 插件,再创建 .py 文件并编写代码,最后按 Ctrl B 运行代码,输出会在控制台中显示。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

Golang在性能和可扩展性方面优于Python。1)Golang的编译型特性和高效并发模型使其在高并发场景下表现出色。2)Python作为解释型语言,执行速度较慢,但通过工具如Cython可优化性能。

在 Visual Studio Code(VSCode)中编写代码简单易行,只需安装 VSCode、创建项目、选择语言、创建文件、编写代码、保存并运行即可。VSCode 的优点包括跨平台、免费开源、强大功能、扩展丰富,以及轻量快速。

在 Notepad 中运行 Python 代码需要安装 Python 可执行文件和 NppExec 插件。安装 Python 并为其添加 PATH 后,在 NppExec 插件中配置命令为“python”、参数为“{CURRENT_DIRECTORY}{FILE_NAME}”,即可在 Notepad 中通过快捷键“F6”运行 Python 代码。
