Python socket Programming Exercise (TCP)

Source: Internet
Author: User

Python can implement socket programming using the socket library. Socket programming is usually divided into the server side and client side, when programming is generally first server re-client, this is the most normal logic, because the functionality is provided by the server, the client must be based on the rules provided by the server to establish the connection and send messages.


Server-side functions:

Function Describe
S.bind (Address) bind a socket to an address, in theaf_inetunder,in tuples (Host,port) in the form of an address.
S.listen (Backlog) Start listening .TCPIncoming connections. BacklogSpecifies the maximum number of connections that the operating system can suspend before a connection is denied. The value is at least1, most applications are set to5you can do it.
S.accept () AcceptTCPConnect and return (conn,address),whichConnis a new socket object that can be used to receive and send data. Addressis the address of the connection client.

The process for the server to establish a TCP connection is as follows:

Socket () Create socket

Bind () Bind address and port

Listen () Start listening for connection requests

Accept () accepts the connection request and returns a new socket for processing subsequent operations,

Recv () and send () to send and receive data

New sockets and swing are followed by close ()->end


Code Exercise:

Import socketfrom time Import ctimehost = "" PORT = 10012ADDR = (host,port) BUFSIZE = 1024s=socket.socket (socket.af_inet, so Cket. SOCK_STREAM) S.bind (ADDR) S.listen (5) while True:print "Waiting for connection ..." conn,addr = s.accept () print ". . Connection from: ", addr and True:data = CONN.RECV (BUFSIZE) if not data:break conn.s End (' [%s]%s '% (CTime (), data)) print [CTime ()], ': ', Dataconn.close () S.close ()


Client functions:

Function Describe
S.connect (Address) connected toAddressSocket at the same place. GeneralAddressThe format is a tuple (Hostname,port), if there is an error in the connection, returnSocket.errorerror.
S.CONNECT_EX (adddress) function andConnect (address)same, but successfully returned0, failed to returnerrnothe value.

The process by which a client establishes a TCP connection:

Socket () Create socket

Connection () Request to establish a connection

Recv () and send () to send and receive data

Close () Socket disconnect->end

Note that the client does not return a new socket after the connection has been established


Code Exercise:

Import socketfrom time Import ctimehost = ' localhost ' PORT = 10012BUFSIZE = 1024ADDR = (host,port) tcpsock =socket.socket (so Cket.af_inet,socket. SOCK_STREAM) Tcpsock.connect (ADDR) while true:data = Raw_input (">") if not data:break tcpsock.send (data data = TCPSOCK.RECV (BUFSIZE) if not data:break print data tcpsock.close ()

The demo is as follows

Client input:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/7B/wKiom1UJjxCSQjnwAADCuuhd_xI045.jpg "title=" C.jpg " Style= "Font-size:16px;float:none;" alt= "wkiom1ujjxcsqjnwaadcuuhd_xi045.jpg"/>


Server-side:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5B/75/wKioL1UJkDiRAsfGAADiDaFNcyw401.jpg "style=" float: none; "title=" s.jpg "alt=" Wkiol1ujkdirasfgaadidafncyw401.jpg "/>

This article is from the "Cotton Learning Notes" blog, please be sure to keep this source http://chenmg.blog.51cto.com/3039876/1622025

Python socket Programming Exercise (TCP)

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.