Python network programming socket

Source: Internet
Author: User

In Python socket programming, TCP and UDP programming is used. The following two different programming methods are used to send information from the client to the server,

 

The server returns the current time and received information of the customer service.

 

 

Let's first implement TCP Programming

TCP server code (tstserv. py ):

 

''' <Br/> created on 2010-12-13 <br/> @ Author: administrator <br/> ''' <br/> from socket import * <br/> from time import ctime <br/> host = ''<br/> Port = 21582 <br/> bufsiz= 1024 <br/> ADDR = (host, port) <br/> tcpsersock = socket (af_inet, sock_stream) <br/> tcpsersock. BIND (ADDR) <br/> tcpsersock. listen (5) <br/> while true: <br/> Print 'Waiting for connection... '<br/> tcpclisock, ADDR = tcpsersock. accept () <br/> Print '... connected from: ', ADDR </P> <p> while true: <br/> DATA = tcpclisock. recv (bufsiz) <br/> if not data: <br/> Break <br/> tcpclisock. send ('[% s] % s' % (ctime (), data) <br/> tcpsersock. close () <br/> tcpclisock. close () <br/> 

 

Tcp client code (tstclnt. py ):

''' <Br/> created on 2010-12-13 <br/> @ Author: administrator <br/> ''' <br/> from socket import * <br/> host = 'localhost' <br/> Port = 21582 <br/> bufsiz = 1024 <br/> ADDR = (host, port) <br/> tcpclisock = socket (af_inet, sock_stream) <br/> tcpclisock. connect (ADDR) <br/> while true: <br/> DATA = raw_input ('>') <br/> if not data: <br/> Break <br/> tcpclisock. send (data) <br/> DATA = tcpclisock. recv (bufsiz) <br/> if not data: <br/> Break <br/> Print data <br/> tcpclisock. close () <br/> 

 

OK. Now let's take a look:

 

 

 

Implement UPD Programming

 

UDP server code (tsuserv. py ):

''' <Br/> created on 2010-12-13 <br/> @ Author: administrator <br/> ''' <br/> from socket import * <br/> from time import ctime <br/> host = ''<br/> Port = 21590 <br/> bufsiz= 1024 <br/> ADDR = (host, port) <br/> udpsersock = socket (af_inet, sock_dgram) <br/> udpsersock. BIND (ADDR) <br/> while true: <br/> Print 'Waiting for message... '<br/> data, ADDR = udpsersock. recvfrom (bufsiz) <br/> udpsersock. sendto ('[% s] % s' % (ctime (), data), ADDR) <br/> Print '... stored ed from and returned to: ', ADDR <br/> udpsersock. close () </P> <p> 

 

UDP client code (tsuclnt. py ):

 

''' <Br/> created on 2010-12-13 <br/> @ Author: administrator <br/> ''' <br/> from socket import * <br/> from time import ctime <br/> host = 'localhost' <br/> Port = 21590 <br/> bufsiz= 1024 <br/> ADDR = (host, port) <br/> udpclisock = socket (af_inet, sock_dgram) <br/> while true: <br/> DATA = raw_input ('> ') <br/> if not data: <br/> Break <br/> Print data <br/> udpclisock. sendto (data, ADDR) <br/> data, ADDR = udpclisock. recvfrom (bufsiz) <br/> if not data: <br/> Break <br/> Print data <br/> udpclisock. close () <br/> 

 

OK.

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.