Learn Python from scratch week 8: basics of network programming (socket)

  • 2020-05-17 05:54:06
  • OfStack

1. Socket programming

(1) introduction to Socket method

Socket is an abstraction for network programming. Usually, we use 1 Socket to mean "1 network link is opened", and open 1 Socket to know the IP address and port number of the target computer, and then specify the protocol type. A socket is the endpoint of a two-way communication channel. Sockets may be in the communication process, between processes on the same machine, or between processes on different computers To create a socket, you must use the socket.socket () method of the Socket module

1 general syntax in socket module:


s = socket.socket(socket_family,socket_type,protocol=0)

(3) introduction of TCP

Most connections are reliable TCP connections. When creating an TCP connection, the client that actively initiates the connection is called the client, and the server that passively responds to the connection is called the server

For example, when we visit sina in a browser, our own computer is the client, and the browser will initiate a connection to sina's server. If the first cut goes well, sina's server accepts our connection, and an TCP connection is established, the subsequent communication is to send web content

(4) TCP programming demo - client

To create an Socket based TCP connection, code demo:


 import socket
 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 s.connect(('www.sina.com.cn',))

After establishing an TCP connection, you can send a request to the server to return the contents of the home page in a text format that conforms to the HTTP standard, then receive the data returned by the server, and finally close the connection

(5) TCP programming demo - server

Server programming is more complex than client programming, where the server process first binds to a port and listens for connections from other clients. If a client connects, the server establishes an Socket connection with that client, and the subsequent communication depends on this Socket connection

Write a simple server program, it receives the client connection, the client sent to the string plus Hello and send back, code demo:


 import socket
 
 Host = 'locakhost'   # Listen to the IP address 
 port =       # Listening port 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)  # Create a socket 
 s.bind(Host,port)    # The binding IP Address and port 
 s.listen()       # To start listening to 
 conn,addr = s.accept() # accept 1 A new connection 
 data = conn.recv() # Receive the client string 
 conn.sendall(data+'Hello') # Send a string to the client 

It is important to note that after one port is bound by one Socket, it cannot be bound by another Socket

(6) introduction to UDP

TCP is to establish a reliable connection, and both sides of the communication can send data in the form of a stream. Compared with TCP, UDP is a connectionless protocol When using the UDP protocol, you do not need to establish a connection, but only need to know the IP address and port number of the other party to send packets directly. But it is not clear whether they will get there. Although it is not reliable to transmit data with UDP, it has the advantage that it is faster than TCP. For data that does not require reliable arrival, UDP protocol can be used

(7) UDP programming demonstration

Data is transmitted through the UDP protocol. Similar to TCP, the communication parties using UDP are divided into client and server. The server first needs to bind the port. Code demonstration:


 s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 s.bind(('...',))  # Port binding 

When the client USES UDP, it still creates Socket based on UDP, but it does not need to call connect(), and directly sends data to the server through sendto (). Code demonstration:


 s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
 for data in ['Michael','Tracy','Sarah']:
   s.sendto(data)

Note that the server bindings to UDP port and TCP port do not conflict with each other, and port 9999 of UDP and port 9999 of TCP can be bound separately

2. Example of TCP programming

Socket is an abstraction for network programming. Usually, we use 1 Socket to mean "a network link is opened", and open 1 Socket to know the IP address and port number of the target computer, and then specify the protocol type.

The client

For example, when we visit sina in a browser, our own computer is the client, and the browser will initiate a link to sina's server. If the first cut goes well, sina's server receives our connection, and an TCP connection is established, the subsequent communication is to send the web content.

So, to create an Socket connection based on TCP, we can do this:


 #  The import socket library 
 import socket
 #  create 1 a socket : 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 #  Establish a connection 
 s.connect(('www.sina.com.cn',))

When creating Socket, AF_INET specifies the use of the IPv4 protocol, or AF_INET6 if you want to use the more advanced IPv6. SOCK_STREAM specifies the use of the stream-oriented TCP protocol, so that an Socket object is created, but no connection has been established.

To initiate an TCP connection, the client must know the IP address and port number of the server. Sina website IP address can use the domain name www. sina. com. cn automatic conversion to IP address, and the standard port 80 Web service.

Therefore, the code of our connection to sina server is as follows:


s = connect(('www.sina.com.cn',80))

Note that the parameter is 1 tuple, containing the address and port number.

After setting up TCP connection, we can send a request to sina server to return the contents of the homepage:


#  Send data: 
s.send('GET / HTTP/1.1\r\nHost:www.sina.com.cn\r\nConnection: close\r\n\r\n')

The TCP connection creates a two-way channel where both parties can send data to each other at the same time. But who will be first and who will be last, and how to coordinate, will depend on the specific agreement. For example, the HTTP protocol states that the client must send the request to the server first, and the server must send the data to the client after receiving it.

The text format must conform to HTTP standard. If the format is ok, then you can receive the data returned by sina server:


 #  Received data: 
 buffer = []
 while True:
   #  Maximum per reception K Byte: 
   d = srecv()
   if d:
     bufferappend(d)
   else:
     break
   data = ''join(buffer)

When the data is received, the recv(max) method is called to receive the specified number of bytes at most once. Therefore, the data is received repeatedly in an while loop until recv () returns empty data, indicating that the data is received and exits the loop.

When we have received the data, we call the close () method to shut down Socket, thus, a complete network communication ends:


#  Close the connection 
s.close()

The data received includes the HTTP header and the webpage itself. We only need to separate the HTTP header from the webpage for 1, print out the HTTP header, and save the content of the webpage to a file:


 import socket
 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 s.connect(('www.sina.com.cn',))
0

Now, just open the sina.html file in your browser and you'll see the sina homepage.

The server

Server programming is more complex than client programming.

The server process first binds a port and listens for connections from other clients. If a client connects, the server establishes an Socket connection with that client, and the subsequent communication depends on this Socket connection.

So, the server opens a fixed port (say, 80) to listen and creates the Socket connection for every client connection. Since the server opens a fixed port (say 80) for listening, the Socket connection is created for each client connection. Because the server will have a large number of connections from clients, the server needs to be able to distinguish which client an Socket connection is bound to. 1 Socket depends on 4 items: server address, server port, client address, and client port to determine 1 Socket.

However, the server also needs to respond to multiple client requests at the same time, so each connection needs a new process or a new thread to process, otherwise, the server can only serve one client at a time.

Let's write a simple server program that takes the client connection, adds Hello to the string sent by the client and sends it back.

First, create an Socket based on the IPv4 and TCP protocols:


 import socket
 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 s.connect(('www.sina.com.cn',))
1

Then, we will bind the listening address and port. The server may have multiple network CARDS that can be bound to the IP address of a particular network card, to all network addresses by 0.0.0.0, or to the local address by 127.0.0.1. 127.0.0.1 is a special IP address, which represents the local address. If bound to this address, the client must be running on the local machine at the same time to connect, that is, the external computer cannot be connected.

The port number needs to be specified in advance. Since the service we wrote is not a standard service, we use the port number 9999. Note that port Numbers less than 1024 must have administrator privileges to bind:


 import socket
 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 s.connect(('www.sina.com.cn',))
2

Next, the call to the listen () method begins listening on the port, and the incoming parameter specifies the maximum number of connections to wait for:


s.listen(5)
print 'Waiting for connection...'

Next, the server program accepts the connection from the client through a permanent loop, accept () waits and returns a client connection:


 import socket
 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 s.connect(('www.sina.com.cn',))
4

Each connection must be processed by creating a new thread (or process). Otherwise, a single thread cannot accept connections from other clients during connection processing:


 import socket
 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 s.connect(('www.sina.com.cn',))
5

After the connection is established, the server first sends a welcome message, then waits for the client data and adds Hello before sending it to the client. If the client sends the exit string, close the connection directly

To test the server program, we need to write one more client program:


 import socket
 
 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
 s.connect(('www.sina.com.cn',))
6

Then we open two command line Windows, one running the server program and the other running the client program, and we can see the effect.

It is important to note that the client program exits when it finishes running, while the server program will run forever. You must exit the program by pressing Ctrl+C.

summary

Socket programming with TCP protocol in Python is 10 minutes simple, for the client, to actively connect the server's IP and the specified port, for the server, to first listen to the port, and then, for each new connection, to create a thread or process to deal with. Usually, the server program runs indefinitely.

After one port is bound by one Socket, it cannot be bound by another Socket.


Related articles: