Today we have a look at TCP programming for Python.
The idea of finding ideas is almost identical to other languages (like Java).
Look at the client first. The basic process is as follows:
- First step: Create a socket
- Step two: Establish a connection
- Step three: Send data
- Fourth step: Read the data sent from the server
- Fifth step: Close the connection
- Sixth step: processing the Received data
The following is a sample of the Python client program for TCP programming. See here for a blog post on network programming in Java
#coding: Utf-8#TCP编程的client程序#编写client程序与其他语言 (such as Java) are almost identical in their thinking. For example, the following#第一步: Create a socketImportSockets=Socket.Socket(Socket. Af_inet,Socket. SOCK_STREAM)#第二步: To establish a connection, the number of references is a tuple, to visit Sina for examples.Connect((' www.sina.com.cn ', the))#80port是Web服务的标准port#第三步: Send datas.Send(b' get/http/1.1\r\n host:www.sina.com.cn\r\nconnection:close\n\r\n ')#第四步: Receiving dataBuffer=[] whileTrue:d=s.recv(1024x768)#recv (max) method. Indicates that only max bytes can be read at a time ifD:buffer.append (d)Else: BreakDate=b"'.Join(buffer)#第五步: Close the connections.Close()#第六步: Processing the data received by the docking#因为接收到的数据包含http头和网页本身. So separate it .Header,html=date.Split(b' \r\n\r\n ',1)Print(Header.decode (' Utf-8 ')) withOpen(' sina.html ',' WB ') as F:f.Write(HTML)
See the server-side program below. Ideas such as the following
- First step: Create a socket
- Step two: Bind the listening address and port, method bind () only receives a tuple
- Step three: Call the Listen () method to start listening on the port, passing in the number of parameters to specify the maximum number of waiting connections
- Fourth step: The server program receives from the client through a permanent loop. Accept () will wait and return a client connection
#python中的server端的程序, the client program used for testing is: tcpclient1.py#coding: Utf-8ImportSocketImportThreading def tcplink(sock,addr):Print' Accept new connection from%s:%s ... '%ADDR) Sock.send (B ' Welcome ') while True: Date=sock.recv (1024x768)if notDateorDate.decode (' Utf-8 ')==' exit ': BreakPrint (Date.decode (' Utf-8 ')) Sock.close () print (' Connection from%s:%s '%ADDR)#第一步: Create a socketS=socket.socket (Socket.af_inet,socket. SOCK_STREAM)#第二步: Bind the listening address and port, method bind () only receives a tupleS.bind ((' 127.0.0.1 ',9999))#第三步: Call the Listen () method to start listening on port. Incoming parameters specify the maximum number of waiting connectionsS.listen (Ten)#第四步: The server program receives a connection from client,accept () that waits and returns a client through a permanent loop while True: Sock,addr=s.accept ()#创建一个新线程来处理TCP链接Threading. Thread (target=tcplink,args= (SOCK,ADDR)). Start ()
Test the server-side program above. We use a simple client program to test. For example, the following
#为tcpServerserver端写一个測试的client程序#coding: Utf-8ImportSocket#第一步: Create a sockets=Socket.Socket(Socket. Af_inet,Socket. SOCK_STREAM)#第二步: Establishing a connections.Connect((' 127.0.0.1 ',9999))#第三步: Send datas.Send(b' Hello world! ')#第三步: Receiving dataDate=s.recv(1024x768)Print(Date.decode (' Utf-8 '))
Just a few days ago the system learned a bit about Git version number control this tool. So, started to run my github, the above code can be downloaded from my github, click here to
The reference address is as follows: specific introduction to TCP programming Http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001432004374523e495f640612f4b08975398796939ec3c000
TCP Programming Learning in Python