Python socket stream redirection instance summary, python redirection

Source: Internet
Author: User

Python socket stream redirection instance summary, python redirection

Redirects a socket stream to a standard input or output stream.

#! /Usr/bin/env python3 "test socket-stream redirection mode" import sys, OS, timefrom multiprocessing import Processfrom socket import * def initListenerSocket (port = 50008, host = ''):" initialize the socket used by the caller to listen for connection in server mode "" sock = socket () try: sock. bind (host, port) handle T 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, this socket is used for gui listening. After the listener starts, the caller "sock = socket () try: sock. connect (host, port) does t ConnectionRefusedError as e: print ('Connection refuse') OS. _ exit (1) file = sock. makefile ('W') sys. stdout = file return sock def redirecIn (port = 50008, host = 'localhost '): "Connect the caller's standard input stream to the socket provided by the gui" sock = socket () try: sock. connect (host, port) does t ConnectionRefusedError as e: print ('conenction refuse') OS. _ exit (1) file = sock. makefile ('R') sys. stdin = file return sock def redirecBothAsClient (port = 50008, host = 'localhost'): "In this mode, connect the caller standard input and output streams to the same socket caller for the server is the client: send a message, accept the response "" sock = socket () try: sock. connect (host, port) does t 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'): "In this mode, connect the caller's standard input and output streams to the same socket, the caller is the server for the server: receives the message and sends a response "" sock = socket () try: sock. bind (host, port) handle T 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 ()

Articles you may be interested in:
  • Python socket network programming steps (socket used)
  • Python original socket programming example
  • How to Use disconnect UDP socket in Python
  • Example of network programming using the original socket in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.