Python socket Flow redirect Instance Rollup

Source: Internet
Author: User
Redirecting a socket stream to a standard input or output stream

#!/usr/bin/env python3 "" "Test Socket-stream redirect Mode" "Import sys,os,timefrom multiprocessing import processfrom socket        Import * def initlistenersocket (port=50008,host= "):" "Initializes the Socket" "" Sock=socket () try for the caller to listen for connections in server mode: Sock.bind ((host,port)) except OSError as E:print (' Address already in use ') os._exit (1) sock.list En (5) conn,addr=sock.accept () return conn def redirecout (port=50008,host= ' localhost '): "" "before accepting other connections fail, connect caller The standard output stream is to a socket, which is used for GUI listening, and after the listener starts, the caller "" "Sock=socket () Try:sock.connect ((host,port)) except Conn Ectionrefusederror as E:print (' connection refuse ') os._exit (1) file=sock.makefile (' W ') Sys.stdout=fil    E return sock def redirecin (port=50008,host= ' localhost '): "" "Connect caller standard input stream to socket" "" Sock=socket () for GUI to provide 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 '): "" In this mode, connect the caller standard input and output streams to The same socket caller is the client for the server: Send message, accept response reply "" "Sock=socket () Try:sock.connect ((host,port)) except Connectio Nrefusederror as E:print (' connection refuse ') os._exit (1) ofile=sock.makefile (' W ') Ifile=sock.makefil    E (' R ') Sys.stdout=ofile sys.stdin=ifile return sock def redirecbothasserver (port=50008,host= ' localhost '): "" " In this mode, the connection caller standard input and output stream to the same socket, the caller for the server is the service side: Accept the message, send Response "" "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.a Ccept () 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 CLI Ent2 (): Time.sleep (1) mypid=os.getpid () redirecin () for I in Range (3): Data=input () print (' Clien    T%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 (' Clie NT%s:%s '% (mypid,data)) Data=input () Sys.stderr.write (' client%s got [%s]\n '% (mypid,data)) def server4 (PO rt=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 c        Lient4 (): 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=initlistene        Rsocket () 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 ()
  • 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.