Python serial 53-UDP, TCP, FTP programming examples

First, the server program requirements are always running, generally used to deal with the cycle of death

1. transformation server version V03 (the main program

Intact, just to modify the program running) 

 

IF  __name__ == " __main__ " : 

    the while True: 

        the try : 

            Print ( " Start Server " ) 

            serverFunc () 

            Print ( " End Server " ) 

        the except Exception AS E: 

            Print (E ) 
        the time.sleep (

 1)

 

 

Two, TCP programming

1. Facing the transmission link, that need to create an instance before each transmission

2. The client and server need to write two programs

3.Server end of the writing process

(1) establish responsible for specific communication socket, the socket is actually only responsible for accepting the request of the other party

(2) address and port bindings

(3) monitor access access socket

(4) interviewed socket, it can be understood in an interview namely the establishment of a communication link path

(5) receiving the content sent to the other party, using the received content receiving socket

(6) If necessary, send each other feedback

(7) via the close link

4.Client end process

(1) establishing a communication socket

(2) other links to setup path with each other

(3) sends the content to the other server

(4) accept each other's feedback

(5) the close link passage

5. Examples

 

Import Socket 


DEF tcp_srv (): 

    our sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # which a parameter which is ipv4, the parameter is a fixed parameter TCP 

    addr = ( " 127.0.0.1 " , 8998) # IP address and port number 

    sock.bind (addr) # bind addr 
    sock.listen () # access socket listener access the while True: # interviewed socket, can be understood as an interview namely the establishment of a communication link path, and the first element of the tuple is assigned to accept returned skt, the second element assigned addr 
        skt, addr = sock.accept () # the first element is a socket, a first element is the sender address # accept each other send content using the received content to accept socket




    

        


        

        MSG = skt.recv (500) # 500 representative of the received buffersize used, the size of content to accept understood 

        MSG = msg.decode () # received bytes is the format of the content, format, str want, to be decoded 

        # following three lines representative of the feedback information 

        RST = " Received MSG: {0} from {}. 1 " .format (MSG, addr) 

        Print (RST) 

        skt.send (rst.encode ()) 

        # Close Socket 

        skt.close () 


IF  the __name__ == " __main__ " : 

    Print ( " Startint tcp Server ....... " ) 

    tcp_srv () 

Print ("Ending tcp server.........")

 

 

 

 

Import socket 


DEF tcp_clt (): 

    our sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) 

    addr = ( " 127.0.0.1 " , 8998 ) 

    sock.connect (addr) # link addr this address, tcp establish this channel 

    msg = " the I AM a Good man " 

    sock.send (msg.encode ()) 

    RST = sock.recv (500) # accept each other feedback 

    Print (rst.decode ()) 

    sock.close () 


IF  the __name__ == " __main__ " : 

    tcp_clt ()

 

Explanation: The first run server-side program once and then run the program four times a client can see the server (port 8998) received four messages sent by the client, and the port each time sent a message are not the same ascending order 53256-53259.

Three, FTP program

FTP (FileTransferProtocal) File Transfer Protocol

Purpose: to customize some of the special service of uploading and downloading files

User Category: FTP server you must have an account

(1) Real Account: Account Registration

(2) Guest accounts: It may be temporary for a certain type of human behavior authorize

(3) Anoymous account: anonymous account, allowing anyone

Fourth, the source

D33_3_LoopOfServer.py

D33_4_TCPSever.py

D33_5_TCPClient.py

https://github.com/ruigege66/Python_learning/blob/master/D33_3_LoopOfServer.py

https://github.com/ruigege66/Python_learning/blob/master/D33_4_TCPSever.py

https://github.com/ruigege66/Python_learning/blob/master/D33_5_TCPClient.py

2.CSDN: https: //blog.csdn.net/weixin_44630050 (Xi Jun Jun Moods do not know - Rui)

3. Park blog: https: //www.cnblogs.com/ruigege0000/

4. Welcomes the focus on micro-channel public number: Fourier transform public personal number, only for learning exchanges, backstage reply "gifts" to get big data learning materials

 

 

Guess you like

Origin www.cnblogs.com/ruigege0000/p/11925336.html