Simple usage of TCP sockets for Python network programming

Source: Internet
Author: User
This article mainly introduces the Python network programming of the TCP socket simple usage, here to share to everyone, the need for friends can refer to the next

The example in this paper describes the simple usage of TCP sockets for Python network programming. Share to everyone for your reference, as follows:

Last semester to learn the computer network, because did not learn python, and Java is smattering, C write up and trouble, so has not really implemented TCP socket programming.

Recently learned Python, and use it to write sockets is very convenient and simple, so of course, try it.

The following code to introduce the simplest TCP program, by the client input data, sent to the server, the server plus time to return to the client


#!/usr/bin/python ' Test TCP server ' from socket import *  #套接字所需要的模块from time import ctime  #时间模块HOST = '  #主机名, is empty, indicating that it can be bound to any valid address. PORT = 21567  #随机选的可用端口BUFSIZ = 1024x768  #发送数据的缓冲区ADDR = (HOST, PORT) Tcpsersock = socket (af_inet, sock_stream)  # The first parameter is socket_family, where the common af_inet is used, and the second parameter is a stream socket, which is the creation of the Tcptcpsersock.bind (ADDR)  #套接字与地址绑定tcpSerSock. Listen (5)  #开始监听, the parameter represents the maximum number of connections allowed while True:  print ' Waiting for connection ... '  tcpclisock, addr = Tcpsersock.accept ()  #打开大门等待客人, Accept returns a tuple (connection,address). The first element is the new socket object, which the server must communicate with the customer, and the second is the customer's address.  print ' ... connected from: ', addr while  True:    data = TCPCLISOCK.RECV (bufsiz)  #洗耳恭听, log guest's words to data    if not data:  #客人话说完了, drop-off    tcpclisock.send (' [%s]%s '% (CTime (), data))  #发送当前时间 and data to guests  tcpclisock.close ()  #送客, close the socket Tcpsersock.close () that communicates with the guest  #因为上面是死循环, it is not necessary here, just to remind everyone to leave the door must be closed!


With two sockets, the first tcpsersock is called a welcome socket, the second tcpclisock is the socket that is really responsible for communicating with the client, and the advantage is that a server can host multiple guests at the same time.

The following is the client's code, very simple:


#!/usr/bin/python ' Test TCP client ' from socket import *host = ' localhost '  #因为我是在同一台机器上运行, so is localhost, different host words, This needs to be changed to server address PORT = 21567BUFSIZ = 1024ADDR = (HOST, PORT) Tcpclisock = socket (af_inet, sock_stream)  # Client only needs one socket Tcpclisock.connect (ADDR)  #发起连接while True:  data = raw_input (' > ')  tcpclisock.send (data)  #发送数据  If not data:    break  data = Tcpclisock.recv (bufsiz)  #接收数据  print datatcpclisock.close ( )

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.