Python server development -- network basics

高洛峰
Release: 2016-10-18 10:52:46
Original
1106 people have browsed it

The network is divided into physical layer, data link layer, network layer, transport layer, session layer, presentation layer and application layer from bottom to top.

HTTP is a high-level protocol, while TCP/IP is a protocol set that includes many sub-protocols. Including: FTP, UDP, TCP protocols at the transport layer, ip protocol at the network layer, etc., high-level protocols such as HTTP, telnet protocol, etc. HTTP is a sub-protocol of TCP/IP.

Socket is the encapsulation and application of the TCP/IP protocol (at the programmer level). It can also be said that TPC/IP protocol is a transport layer protocol, which mainly solves how data is transmitted in the network, while HTTP is an application layer protocol, which mainly solves how to package data.

When we transmit data, we can only use the (transport layer) TCP/IP protocol, but in that case, without the application layer, the data content cannot be identified. If you want to make the transmitted data meaningful, you must use the application There are many application layer protocols, such as HTTP, FTP, TELNET, etc. You can also define your own application layer protocols. WEB uses HTTP protocol as the application layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send it to the network.

What is the socket we usually talk about most? In fact, socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but a calling interface (API). Through Socket, we can use TCP/IP protocol. In fact, Socket is not necessarily related to the TCP/IP protocol. When the Socket programming interface was designed, it was hoped that it could also adapt to other network protocols. Therefore, the emergence of Socket only makes it easier for programmers to use the TCP/IP protocol stack. It is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know, such as create, listen, connect, accept, send, read and write etc.

TCP/IP is just a protocol stack, just like the operating mechanism of the operating system, it must be implemented concretely, and it must also provide an external operation interface. Just like the operating system provides a standard programming interface, such as the win32 programming interface, TCP/IP also provides an interface that programmers can use for network development. This is the Socket programming interface.

There is a more vivid description: HTTP is a car, which provides a specific form of encapsulating or displaying data; Socket is an engine, which provides the capability of network communication.

In fact, the TCP of the transport layer is based on the IP protocol of the network layer, and the HTTP protocol of the application layer is based on the TCP protocol of the transport layer. Socket itself is not a protocol. As mentioned above, it only provides An interface for TCP or UDP programming.

Steps to establish a network connection using Socket:

Establishing a Socket connection requires at least a pair of sockets, one of which runs on the client, called ClientSocket, and the other runs on the server, called ServerSocket.

The connection process between sockets is divided into three steps: server monitoring, client request, and connection confirmation.

1. Server monitoring: The server-side socket does not locate the specific client socket, but is in a state of waiting for connection, monitoring the network status in real time, and waiting for the client's connection request.

2. Client request: refers to the client's socket making a connection request, and the target to be connected is the server's socket. To do this, the client's socket must first describe the server's socket to which it wants to connect, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

3. Connection confirmation: When the server-side socket listens or receives a connection request from the client socket, it responds to the client socket's request, establishes a new thread, and sends the description of the server-side socket. To the client, once the client confirms this description, the connection is officially established between the two parties. The server-side socket continues to be in the listening state and continues to receive connection requests from other client sockets.

Characteristics of HTTP links

HTTP protocol is the Hypertext Transfer Protocol, which is the basis of Web networking and one of the commonly used protocols for mobile phone networking. The HTTP protocol is an application built on the TCP protocol.

The most notable feature of HTTP connections is that each request sent by the client requires the server to send back a response. After the request is completed, the connection will be actively released. The process from establishing a connection to closing the connection is called "a connection".

The Internet Protocol

IP is a 32-bit unsigned integer. The IP address is mapped to the domain name through the DNS (Domain Name System) database

#!/usr/bin/env python
# Foundations of Python Network Programming - Chapter 1 - getname.py
import socket
hostname = 'google.com'
addr = socket.gethostbyname(hostname)
print 'The address of', hostname, 'is', addr
# The address of google.com is 173.194.72.113
Copy after login

Python network programming:

Python provides all methods to access the Socket interface of the underlying operating system, and also provides a set of encrypted and authenticated communication services, SSL/TLS .

Sockets is actually a file descriptor. Different from local files, it connects to a file on the network.

1. Create a UDP local connection:

#!/usr/bin/env python
import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
MAX = 65535
PORT = 1060
if sys.argv[1:] == ['server']:
    s.bind(('127.0.0.1', PORT))
    print 'Listening at', s.getsockname()
    while True:
        data, address = s.recvfrom(MAX)
        print 'The client at', address, 'says', repr(data)             
     s.sendto('Your data was %d bytes' % len(data), address)
elif sys.argv[1:] == ['client']:
        print 'Address before sending:', s.getsockname()
        s.sendto('This is my message', ('127.0.0.1', PORT))
        print 'Address after sending', s.getsockname()
        data, address = s.recvfrom(MAX) # overly promiscuous - see text!
        print 'The server', address, 'says', repr(data)
else:
    print >>sys.stderr, 'usage: udp_local.py server|client'
Copy after login

Run this code:

python filename.py server
#Listening at ('127.0.0.1', 1060)
#Address before sending: ('0.0.0.0', 0)
#Address after sending ('0.0.0.0', 62892)
#The server ('127.0.0.1', 1060) says 'Your data was 18 bytes'
python filename.py client
#The client at ('127.0.0.1', 62892) says 'This is my message'
Copy after login

2. Create a remote connection and verify the received information:

import random, socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
MAX = 65535
PORT = 1060
if 2 <= len(sys.argv) <= 3 and sys.argv[1] == &#39;server&#39;:
    interface = sys.argv[2] if len(sys.argv) > 2 else &#39;&#39;
    s.bind((interface, PORT))
    print &#39;Listening at&#39;, s.getsockname()
    while True:
        data, address = s.recvfrom(MAX)
        if random.randint(0, 1):
            print &#39;The client at&#39;, address, &#39;says:&#39;, repr(data)
            s.sendto(&#39;Your data was %d bytes&#39; % len(data), address)
        else:
            print &#39;Pretending to drop packet from&#39;, address
elif len(sys.argv) == 3 and sys.argv[1] == &#39;client&#39;:
    hostname = sys.argv[2]
    s.connect((hostname, PORT))
    print &#39;Client socket name is&#39;, s.getsockname()
    delay = 0.1
    while True:
        s.send(&#39;This is another message&#39;)
        print &#39;Waiting up to&#39;, delay, &#39;seconds for a reply&#39;
        s.settimeout(delay)
        try:
            data = s.recv(MAX)
        except socket.timeout:
            delay *= 2  # wait even longer for the next request
            if delay > 2.0:
                raise RuntimeError(&#39;I think the server is down&#39;)
        else:
            break   # we are done, and can stop looping
              
    print &#39;The server says&#39;, repr(data)
else:
    print >>sys.stderr, &#39;usage: udp_remote.py server [ <interface> ]&#39;
    print >>sys.stderr, &#39;   or: udp_remote.py client <host>&#39;
    sys.exit(2)
Copy after login

这里的s.connect((hostname, PORT))方法,可以让我们不用每次都调用s.sendto('This is my message', ('127.0.0.1', PORT))。直接调用

s.send('This is another message')。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template