Python3 Network Programming

Source: Internet
Author: User

Although people are now familiar with the Internet, the advent of computer networks is much earlier than the Internet.

Computers in order to network, it is necessary to specify the communication protocol, the early computer network, are the manufacturers themselves set a protocol, IBM, Apple and Microsoft have their own network protocols, incompatible, this is like a group of people speak English, some speak Chinese, some speak German, People who speak the same language can communicate, but not between different languages.

In order to connect all the different types of computers in the world, it is necessary to stipulate a set of global protocols, in order to achieve the goal of the Internet, Internet Protocol cluster (Internet Protocol Suite) is the Universal protocol standard. The internet is a combination of inter and net two words, the original intention is to connect "network" network, with the Internet, any private network, as long as the support of this Protocol, can be linked to the Internet.

Because the Internet Protocol contains hundreds of protocol standards, but the most important two protocols are TCP and IP protocol, so everyone put the Internet Protocol referred to as TCP/IP protocol.

Communication, both parties must know each other's identity, such as email must know the other's e-mail address. The unique identity of each computer on the Internet is the IP address, similar 123.123.123.123 . If a computer is connected to two or more networks, such as a router, it will have two or more IP addresses, so the IP address is actually the computer's network interface, usually the NIC.

The IP protocol is responsible for sending data from one computer to another on the network. The data is split into a small piece and then sent out via IP packets. Because of the complexity of the Internet link, there are often multiple lines between the two computers, so the router is responsible for deciding how to forward an IP packet. The IP packet is characterized by the number of routes sent by the block, but not guaranteed to arrive, and not guaranteed in order.

The IP address is actually a 32-bit integer (called a IPv4), and the IP address represented by the string is, in 192.168.0.1 effect, a 32-bit integer grouped by 8 digits, intended for readability.

The IPV6 address is actually a 128-bit integer, which is an upgraded version of the currently used IPV4, similar to a string representation 2001:0db8:85a3:0042:1000:8a2e:0370:7334 .

The TCP protocol is built on top of the IP protocol. The TCP protocol is responsible for establishing a reliable connection between two computers to ensure that packets arrive sequentially. The TCP co-assembly establishes the connection by shaking hands, then numbering each IP packet, ensuring that the other party receives it sequentially, and automatically re-sends if the package is lost.

Many of the more advanced protocols that are commonly used are based on the TCP protocol, such as the HTTP protocol used for the browser, the SMTP protocol that sends the message, and so on.

An IP packet contains both the source and destination IP addresses, the source and destination ports, in addition to the data to be transferred.

What does a port do? When communicating with two computers, it is not enough to send only the IP address because there are multiple network programs running on the same computer. After an IP packet comes in, whether it is to the browser or QQ, you need a port number to differentiate. Each network program requests a unique port number for the operating system, so that two processes that establish a network connection between the two computers require their respective IP addresses and their respective port numbers.

A process may also establish links to multiple computers at the same time, so it will request many ports.

Tcp

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.

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.

So, we're going to create a TCP connection-based socket that can do this:

# To Import the socket library: Import Socket # Create a socket:s = socket.socket (socket.af_inet, socket. SOCK_STREAM)#  Establish connection:s.connect (('www.sina.com.cn', 80))

Socketwhen created, AF_INET specifies that the IPV4 protocol is used, and if a more advanced IPv6 is used, it is specified as AF_INET6 . SOCK_STREAMspecifies that a stream-oriented TCP protocol is used so that an Socket object is created successfully, but no connection has been established.

To proactively initiate a TCP connection, the client must know the IP address and port number of the server. The IP address of Sina website can be www.sina.com.cn converted to IP address automatically with domain name, but how to know the port number of Sina server?

The answer is that the port number must be fixed as a server and what services are provided. Because we want to access the Web page, the server that Sina provides Web services must pin the port number to the 80 ports, because the 80 port is the standard port of the Web service. Other services have a corresponding standard port number, such as the SMTP service is a port, the 25 FTP service is a 21 port, and so on. Port number less than 1024 is the Internet standard service ports, the port number is greater than 1024, can be used arbitrarily.

So our code to connect to the Sina server is as follows:

S.connect ('www.sina.com.cn', 80)

Note The parameter is one tuple that contains the address and port number.

After establishing the TCP connection, we can send a request to the Sina server and ask to return the contents of the homepage:

# Send data:s.send (b'get/http/1.1\r\nhost:www.sina.com.cn\r\nconnection:close\r\n\r\n ')

A TCP connection creates a bidirectional channel in which both parties can send data to each other. However, who first sent after the hair, how to coordinate, according to the specific agreement to decide. For example, the HTTP protocol specifies that the client must send a request to the server before the server sends the data to the client.

Send the text format must conform to the HTTP standard, if the format is not a problem, then you can receive the data returned by Sina server:

# Receive data:buffer = [] while True:    #  receives up to 1k bytes at a time:    d = s.recv ( 1024x768)    if  d:        buffer.append (d)    Else:          Break  = b'. Join (buffer)

When the data is received, the method is called to recv(max) receive a specified number of bytes at a time, so it is received repeatedly in a while loop until the recv() empty data is returned, indicating that the receive is complete and exits the loop.

When we have finished receiving the data, we call the close() method to close the socket so that a complete network communication is over:

# Close connection:S.close ()

The data received include the HTTP header and the Web page itself, we just need to separate the HTTP header and Web page, the HTTP header print out, the content of the Web page to save to the file:

Header, html = data.split (b'\r\n\r\n', 1)print(Header.decode ('  utf-8')#  writes the received data to a file: withopen ('sina.html  "wb") as F:    f.write (HTML)

Now, just open the file in the browser sina.html , you can see the homepage of Sina.

Server

Server programming is a bit more complicated than client programming.

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.

Let's write a simple server program that receives the client connection and sends back the string that the client sent over Hello .

First, create a socket based on the IPV4 and TCP protocol:

s = socket.socket (socket.af_inet, socket. SOCK_STREAM)

Then we want to bind the listening address and port. The server may have more than one network card, can be bound to the IP address of a network card, can also be 0.0.0.0 bound to all network addresses, can also be used 127.0.0.1 to bind to the native address. 127.0.0.1is a special IP address that represents the native address, if bound to this address, the client must be running at the same time in order to connect, that is, the external computer cannot connect in.

The port number needs to be specified beforehand. Because we write this service is not a standard service, so use 9999 this port number. Note that the less-than 1024 port number must have administrator privileges to bind:

# Listening Port:s.bind ('127.0.0.1', 9999)

Immediately thereafter, the calling listen() method starts listening on the port, and the incoming parameter specifies the maximum number of waiting connections:

S.listen (5)print('waiting for connection ... ')

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

 while True:     # accept a new connection:    Sock, addr = s.accept ()    #  creates a new thread to handle the TCP connection:    t = Threading. Thread (Target=tcplink, args=(sock, addr))    T.start ()

Each connection must create a new thread (or process) to handle, or the single thread cannot accept connections from other clients during the process of processing the connection:

defTcplink (sock, addr):Print('Accept New connection from%s:%s ...'%addr) sock.send (b'welcome!')     whileTrue:data= SOCK.RECV (1024) Time.sleep (1)        if  notDataorData.decode ('Utf-8') =='Exit':             BreakSock.send (('Hello,%s!.'% Data.decode ('Utf-8'). Encode ('Utf-8') ) Sock.close ()Print('Connection from%s:%s closed.'% addr)

After the connection is established, the server first sends a welcome message, then waits for the client data, plus sends it back to the Hello client. If the client sends a exit string, the connection is closed directly.

To test this server program, we also need to write a client program:

s =Socket.socket (socket.af_inet, socket. SOCK_STREAM)#To establish a connection:S.connect (('127.0.0.1', 9999))#To receive a welcome message:Print(S.recv (1024x768). Decode ('Utf-8')) forDatainch[b'Michael'B'Tracy'B'Sarah']:    #Send data:s.send (data)Print(S.recv (1024x768). Decode ('Utf-8')) S.send (b'Exit') S.close ()

We need to open two command-line windows, one running the server program, the other running the client program, you can see the effect:

Note that the client program runs out, and the server program will run forever, you must press CTRL + C to exit the program.

Summary

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

The same port, after being bound by a socket, cannot be bound by another socket.

UDP programming

TCP is to establish a reliable connection, and both sides of the communication can send data in the form of a stream. The relative tcp,udp is a non-connection oriented protocol.

When using the UDP protocol, you do not need to establish a connection, just need to know the other side's IP address and port number, you can directly send packets. But I don't know if I can get there.

Although the transmission of data with UDP is unreliable, its advantage is that it is faster than TCP, and the UDP protocol can be used for data that does not require reliable arrival.

Let's take a look at how the data is transmitted over the UDP protocol. Like TCP, both sides of the communication using UDP are also divided into clients and servers. The server first needs to bind the port:

s = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)#  bound port:s.bind (('127.0.0.1', 9999))

When a socket is created, the SOCK_DGRAM type of the socket specified is UDP. The binding port is the same as TCP, but does not require a listen() method to be called, but instead receives data from any client directly:

Print ('Bind UDP on 9999 ... '  while True    :#  receives data:    addr = S.recvfrom (1024x768)     Print('Received from%s:%s. ' % addr)    s.sendto (b'Hello,%s! ' % data, addr)

recvfrom()The method returns the address and port of the data and the client, so that when the server receives the data, it can send the data to the sendto() client with UDP directly.

Note that multiple threads are omitted here, because this example is simple.

When the client uses UDP, the UDP-based socket is still created first, and then, without calling connect() , directly by sendto() sending data to the server:

s =Socket.socket (socket.af_inet, socket. SOCK_DGRAM) forDatainch[b'Michael'B'Tracy'B'Sarah']:    #Send data:S.sendto (Data, ('127.0.0.1', 9999))    #Receive data:    Print(S.recv (1024x768). Decode ('Utf-8') ) S.close ()

The method is still called to receive data from the server recv() .

The server and client tests are still started with two command lines, with the following results:

Summary

UDP is used similar to TCP, but does not require a connection to be established. In addition, the server-bound UDP port and TCP port do not conflict, that is, UDP 9999 port and TCP 9999 port can be bound separately.

Python3 Network Programming

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.