将套接字流重定向到标准输入或输出流
#!/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()

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to solve the problem of Jieba word segmentation in scenic spot comment analysis? When we are conducting scenic spot comments and analysis, we often use the jieba word segmentation tool to process the text...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function