Python network programming (VI)

Source: Internet
Author: User

TCP Communication Model

TCP Server

In the program, if you want to complete the functionality of a TCP server, the following process is required:

    1. Socket to create a socket
    2. Bind bind IP and port
    3. Listen make the socket a passive link
    4. Accept the link waiting for the client
    5. Recv/send Receive Send data

A very simple TCP server is as follows:

#Coding=utf-8 fromSocketImport*#Create socketTcpsersocket =socket (af_inet, sock_stream)#binding Local InformationAddress = ("', 7788) Tcpsersocket.bind (address)#socket created with socket the default property is active, use listen to make it passive, so you can receive links from othersTcpsersocket.listen (5)#If there is a new client to link to the server, then a new socket is created specifically for this client server#Newsocket is used to service this client#Tcpsersocket can be saved to wait for links to other new clientsNewsocket, clientaddr =tcpsersocket.accept ()#receive data sent from each other, maximum 1024 bytes receivedRecvData = NEWSOCKET.RECV (1024)Print 'the data received are:', RecvData#send some data to the clientNewsocket.send ("Thank you!")#closing the socket for this client service, as long as it is turned off, means that it can no longer serve the client, and if service is required, it can only reconnect againnewsocket.close ()#turning off the listener socket, as long as the socket is closed, means that the entire program can no longer receive any new client connectionsTcpsersocket.close ()
TCP Client TCP client build process

TCP client is much simpler than the server side, if the server is the need to buy their own mobile phones, check the phone card, set the ringtone, waiting for others to call the process, then the client just need to find a telephone booth, pick up the phone call, the process is much less

Example code:

#Coding=utf-8 fromSocketImport*#Create socketTcpclientsocket =socket (af_inet, sock_stream)#Linked serverSERADDR = ('192.168.1.102', 7788) Tcpclientsocket.connect (seraddr)#Prompt user for data entrySendData = Raw_input ("Please enter the data you want to send:") tcpclientsocket.send (senddata)#receive data sent from each other, maximum 1024 bytes receivedRecvData = TCPCLIENTSOCKET.RECV (1024)Print 'the data received are:', RecvData#Close SocketTcpclientsocket.close ()

Python network programming (VI)

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.