Recently in the Liaoche Teacher's basic tutorial (http://www.liaoxuefeng.com/), today implemented a simple Python socket network programming.
1. Socket Network Programming
Sockets are an abstract concept of network programming. Usually we use a socket to indicate "open a network link", while opening a socket needs to know the destination computer's IP address and port number, and then specify the protocol type.
2. Client
Most connections are reliable TCP connections. When you create a TCP connection, you initiate a connected call to the client, and the passive response is called the server. For example, when we visit Sina in a browser, our own computer is the client, and the browser initiates a connection to Sina's server. If all goes well, Sina's server accepts our connection, a TCP connection is built up, and the subsequent communication is to send the Web content.
3. Socket communication between server and client
The main implementation of the server and client socket communication between:
Server
The server process first binds a port and listens for connections from other clients. If a client connects, the server establishes a socket connection to the client, and the subsequent communication is connected to the socket.
Therefore, the server opens a fixed port (for example, 80) to listen, creating the socket connection for each client connection. Because the server will have a large number of connections from the client, the server is able to differentiate between which client and which clients the socket connection is bound to. A socket relies on 4 items: server address, server port, client address, client port to uniquely identify a socket.
However, the server also needs to respond to requests from multiple clients at the same time, so each connection requires a new process or a new thread to handle, otherwise the server can only serve one client at a time.
(1) Introduction module
1 # -*-coding:utf-8-*- 2 """ 3 Created on Mon Mar 22:28:29 4 @author: ZHANGHC 5 """ 6 # Introducing Modules 7 Import Socket 8 Import Threading 9 Import Time
(2) Creating sockets and listening to ports
1 # Create socket 2 s=socket.socket (socket.af_inet,socket. SOCK_STREAM)34# listening Port 5 s.bind ('127.0.0.1 ', 9999))6 s.listen (5)7print' Waiting for connection ... '
(3) server-side create loop to permanently listen for connections from clients
1 while True: 2 3 # accept a new connection 4 Sock,addr=s.accept ()5 6 # Create a new thread to handle TCP connections 7 t=threading. Thread (Target=tcplink (SOCK,ADDR))
(4) Create a TCP connection function, creating a new thread per connection
1 defTcplink (sock,addr):2 Print 'Accept New connection from%s:%s ...'%Addr3Sock.send ('welcome!')4 whileTrue:5DATA=SOCK.RECV (1024)6Time.sleep (1)7 ifdata=='Exit' or notData:8 Break9Sock.send ('hello,%s!'%data)Ten sock.close () One Print 'Connection from%s:%s closed.'%addr
The client program is as follows:
1 #-*-coding:utf-8-*-2 """3 Created on Mon Mar 22:40:414 5 @author: ZHANGHC6 """7 8 ImportSocket9 Tens=Socket.socket (Socket.af_inet,socket. SOCK_STREAM) One AS.connect (('127.0.0.1', 9999)) - - PrintS.RECV (1024) the - forDatainch['Zhang','Liu','Wang']: - s.send (data) - PrintS.RECV (1024) + -S.send ('Exit') +S.close ()
Finally, you need to start two programs, the server and the client to verify that the results are as follows
Python TCP Communication Network programming