Python network programming TCP/UDP

Introduction to TCP/IP

    The Internet protocol contains hundreds of protocol standards, but the two most important protocols are the TCP and IP protocols. Therefore, the Internet protocol is referred to as the TCP/IP protocol.

    When communicating, both parties must know the identity of the other party, just as sending an email must know the other party's email address. The unique identification 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 at the same time, such as a router, it will have two or more IP addresses, so the IP address corresponds to the computer's network interface, usually a network card.

    The IP protocol is responsible for sending data from one computer to another over the network. Data is split into small pieces and sent out via IP packets. Due to the complexity of the Internet link, there are often multiple lines between two computers. Therefore, the router is responsible for deciding how to forward an IP packet. The characteristics of IP packets are that they are sent in blocks and are routed through multiple routes, but there is no guarantee that they can arrive, nor are they guaranteed to arrive in sequence.

    An IP address is actually a 32-bit integer (called IPv4), and an IP address represented by a string is 192.168.0.1actually a 32-bit integer grouped into 8-bit numbers for ease of reading.

    An IPv6 address is actually a 128-bit integer, which is an upgraded version of the currently used IPv4, represented as a string similar to 2001:0db8:85a3:0042:1000:8a2e:0370:7334.    

    The TCP protocol is based on the IP protocol. The TCP protocol is responsible for establishing a reliable connection between two computers, ensuring that packets arrive in order. The TCP protocol will establish a connection through a handshake, and then number each IP packet to ensure that the other party receives it in order. If the packet is lost, it will be automatically resent.

    Many commonly used higher-level protocols are based on the TCP protocol, such as the HTTP protocol for browsers, the SMTP protocol for sending mail, and so on.

    In addition to the data to be transmitted, a TCP packet also contains source IP address and destination IP address, source port and destination port.

    What does the port do? When two computers communicate, only sending IP addresses is not enough, because there are multiple network programs running on the same computer. After a TCP packet arrives, it needs the port number to distinguish whether it is delivered to the browser or QQ. Each network program applies to the operating system for a unique port number, so that two processes need their own IP addresses and their own port numbers to establish a network connection between two computers.

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

TCP programming

client

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

    For example, when we visit Sina in a browser, our own computer is the client, and the browser will actively initiate a connection to Sina's server. If all goes well, Sina's server accepts our connection, a TCP connection is established, and the next communication is to send web page content.

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

# 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))

    When creating Socket, AF_INETspecify to use the IPv4 protocol, or if you want to use the more advanced IPv6, specify it AF_INET6. SOCK_STREAMSpecifies to use the stream-oriented TCP protocol, so that an Socketobject has been created successfully, but a connection has not yet been established.

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

    作为服务器,提供什么样的服务,端口号就必须固定下来。由于我们想要访问网页,因此新浪提供网页服务的服务器必须把端口号固定在80端口,因为80端口是Web服务的标准端口。其他服务都有对应的标准端口号,例如SMTP服务是25端口,FTP服务是21端口,等等。端口号小于1024的是Internet标准服务的端口,端口号大于1024的,可以任意使用。

因此,我们连接新浪服务器的代码如下:

s.connect(('www.sina.com.cn', 80))
    注意:参数是一个 tuple ,包含地址和端口号。

建立TCP连接后,我们就可以向新浪服务器发送请求,要求返回首页的内容:

# 发送数据:
s.send(b'GET / HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection: close\r\n\r\n')

    TCP连接创建的是双向通道,双方都可以同时给对方发数据。但是谁先发谁后发,怎么协调,要根据具体的协议来决定。例如,HTTP协议规定客户端必须先发请求给服务器,服务器收到后才发数据给客户端。

    发送的文本格式必须符合HTTP标准,如果格式没问题,接下来就可以接收新浪服务器返回的数据了:

# 接收数据:
buffer = []
while True:
    # 每次最多接收1k字节:
    d = s.recv(1024)
    if d:
        buffer.append(d)
    else:
        break
data = b''.join(buffer)

    接收数据时,调用recv(max)方法,一次最多接收指定的字节数,因此,在一个while循环中反复接收,直到recv()返回空数据,表示接收完毕,退出循环。

    当我们接收完数据后,调用close()方法关闭Socket,这样,一次完整的网络通信就结束了:

# 关闭连接:
s.close()
    接收到的数据包括HTTP头和网页本身,我们只需要把HTTP头和网页分离一下,把HTTP头打印出来,网页内容保存到文件:
header, html = data.split(b'\r\n\r\n', 1)
print(header.decode('utf-8'))
# 把接收的数据写入文件:
with open('sina.html', 'wb') as f:
    f.write(html)

    现在,只需要在浏览器中打开这个sina.html文件,就可以看到新浪的首页了。

服务器

    服务器进程首先要绑定一个端口并监听来自其他客户端的连接。如果某个客户端连接过来了,服务器就与该客户端建立Socket连接,随后的通信就靠这个Socket连接了。

    所以,服务器会打开固定端口(比如80)监听,每来一个客户端连接,就创建该Socket连接。由于服务器会有大量来自客户端的连接,所以,服务器要能够区分一个Socket连接是和哪个客户端绑定的。一个Socket依赖4项:服务器地址、服务器端口、客户端地址、客户端端口来唯一确定一个Socket。

    但是服务器还需要同时响应多个客户端的请求,所以,每个连接都需要一个新的进程或者新的线程来处理,否则,服务器一次就只能服务一个客户端了。

编写一个简单的服务器程序,它接收客户端连接,把客户端发过来的字符串加上Hello再发回去。

    首先创建socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #创建一个基于IPv4和TCP协议的Socket

    然后,需要绑定监听的地址和端口。服务器可能有多块网卡,可以绑定到某一块网卡的IP地址上,也可以用0.0.0.0绑定到所有的网络地址,还可以用127.0.0.1绑定到本机地址。127.0.0.1是一个特殊的IP地址,表示本机地址,如果绑定到这个地址,客户端必须同时在本机运行才能连接,也就是说,外部的计算机无法连接进来。

    端口号需要预先指定。因为我们写的这个服务不是标准服务,所以用9999这个端口号。请注意,小于1024的端口号必须要有管理员权限才能绑定:

# 监听端口:
s.bind(('127.0.0.1', 9999))
    再调用 listen() 方法开始监听端口,传入的参数指定等待连接的最大数量:
s.listen(5)
print('Waiting for connection...')
服务器程序通过一个永久循环来接受来自客户端的连接, accept() 会等待并返回一个客户端的连接:
while True:
    # 接受一个新连接:
    sock, addr = s.accept()
    # 创建新线程来处理TCP连接:
    t = threading.Thread(target=tcplink, args=(sock, addr))
    t.start()
每个连接都必须创建新线程(或进程)来处理,否则,单线程在处理连接的过程中,无法接受其他客户端的连接:
def tcplink(sock, addr):
    print('Accept new connection from %s:%s...' % addr)
    sock.send(b'Welcome!')
    while True:
        data = sock.recv(1024)
        time.sleep(1)
        if not data or data.decode('utf-8') == 'exit':
            break
        sock.send(('Hello, %s!' % data.decode('utf-8')).encode('utf-8'))
    sock.close()
    print('Connection from %s:%s closed.' % addr)

    连接建立后,服务器首先发一条欢迎消息,然后等待客户端数据,并加上Hello再发送给客户端。如果客户端发送了exit字符串,就直接关闭连接。

    要测试这个服务器程序,我们还需要编写一个客户端程序:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 建立连接:
s.connect(('127.0.0.1', 9999))
# 接收欢迎消息:
print(s.recv(1024).decode('utf-8'))
for data in [b'Michael', b'Tracy', b'Sarah']:
    # 发送数据:
    s.send(data)
    print(s.recv(1024).decode('utf-8'))
s.send(b'exit')
s.close()
import socket
import threading
import time

    运行服务器程序和客户端程序:

----------------------------

小结:

    用TCP协议进行Socket编程在Python中十分简单,对于客户端,要主动连接服务器的IP和指定端口,对于服务器,要首先监听指定端口,然后,对每一个新的连接,创建一个线程或进程来处理。通常,服务器程序会无限运行下去。

    同一个端口,被一个Socket绑定了以后,就不能被别的Socket绑定了。

UDP编程

    TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据。相对TCP,UDP则是面向无连接的协议。

    使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号,就可以直接发数据包。但是,能不能到达就不知道了。

    虽然用UDP传输数据不可靠,但它的优点是和TCP比,速度快,对于不要求可靠到达的数据,就可以使用UDP协议。

    我们来看看如何通过UDP协议传输数据。和TCP类似,使用UDP的通信双方也分为客户端和服务器。服务器首先需要绑定端口:

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 绑定端口:
s.bind(('127.0.0.1', 9999))
    创建Socket时, SOCK_DGRAM 指定了这个Socket的类型是UDP。绑定端口和TCP一样,但是不需要调用 listen() 方法,而是直接接收来自任何客户端的数据:
print('Bind UDP on 9999...')
while True:
    # 接收数据:
    data, addr = s.recvfrom(1024)
    print('Received from %s:%s.' % addr)
    s.sendto(b'Hello, %s!' % data, addr)

recvfrom()方法返回数据和客户端的地址与端口,这样,服务器收到数据后,直接调用sendto()就可以把数据用UDP发给客户端。

注意这里省掉了多线程,因为这个例子很简单。

客户端使用UDP时,首先仍然创建基于UDP的Socket,然后,不需要调用connect(),直接通过sendto()给服务器发数据:

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
for data in [b'Michael', b'Tracy', b'Sarah']:
    # 发送数据:
    s.sendto(data, ('127.0.0.1', 9999))
    # 接收数据:
    print(s.recv(1024).decode('utf-8'))
s.close()
从服务器接收数据仍然调用 recv() 方法。

仍然分别启动服务器和客户端测试,结果如下:

----

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326000591&siteId=291194637