Original implementation VS framework implementation

“It’s useful to have an advanced tool like a framework, but the basics will keep you going forever. Don’t let the tools limit you.” In today’s era of Python server frameworks (frameworks such as Django, Twisted, Web.py, etc.), writing a server from the underlying socket seems like an unwieldy approach.

The point of the framework is to mask the underlying details, provide a more developer-friendly API, and handle layout issues such as MVC. The framework allows us to quickly build a full-fledged Python server. However, the framework itself depends on the underlying layer (socket, for example). Understanding the underlying sockets not only helps us better use the framework, but also enables us to understand how the framework is designed.

Furthermore, “if you have a good knowledge of low-level socket programming and other systems programming, you can design and develop your own framework. If you can build a full Python server from the socket base, support user-level protocols, handle model-View-Control, threading, etc., and put together a clean set of functions or classes, By presenting it to the user as an interface (API), you’ve designed a framework. “

We have seen:

Many successful websites are developed quickly in dynamic languages (such as Python, Ruby, or PHP, such as Twitter and Facebook), then converted to more efficient languages such as C and JAVA, allowing the server to handle the billions of requests it receives every day more efficiently. At such times, the underlying layer becomes much more important than the framework.

Preparation: TCP/IP and socket

We need to have some understanding of network transmission, especially TCP/IP protocol and socket. Socket is a method of interprocess communication (see Linux interprocess Communication), which is an upper layer interface based on network transport protocols. There are many types of sockets, such as TCP or UDP (two network transport protocols). The TCP socket is the most commonly used.

A socket interface is actually a system call provided by the operating system. A TCP socket is similar to a duplex PIPE. One process writes or reads a stream of text to one end of the socket, while another process reads or writes from the other end of the socket. In particular, the two processes that establish socket communication can belong to two different computers.

The so-called TCP protocol provides some communication rules to effectively realize the above process communication process in the network environment. Duplex pipes live on the same computer, so there is no need to distinguish between the addresses of the machines on which the two processes are running, whereas sockets must contain address information for network communication.

A socket contains four addresses: the IP addresses of two computers and the ports used by two processes. IP addresses are used to locate computers, and ports are used to locate processes (you can have multiple processes on a computer using different ports).

TCP socket

On the Internet, we can use a computer as a server. The server opens its own port and passively waits for other computers to connect. When other computers, as clients, actively use sockets to connect to the server, the server provides services to the client. Request sina webpage

Import the socket module
import  socket

Create a client socket
Parameter 1: Use socket.AF_INET to indicate the version of the IP protocol used. Socket. AF_INET is ipv4, and socket.AF_INET6 is ipv6
Parameter 2: SOCK_STREAM indicates that data is transferred as a stream
clientSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

# Connect to server -- Say hello
# address is the server address. This address contains the IP address and port number. The first parameter is the IP address. The second is the port number
# http://www.sina.com.cn/ domain name -- -- -- -- -- -- -- -- -- -- > > DNS domain name server IP address
clientSocket.connect(("www.baidu.com.cn", 80))# make a request to Sina
Data in bytes is required
clientSocket.send(b"The GET/HTTP/1.1\r\nHost:www.baidu.com.cn\r\n\r\n")

Create a list to receive all data
listData = []

Write data to a file
# wf = open("sina.html","wb")



The size of the data to be received
while True:
    content = clientSocket.recv(1024)
    if len(content) == 0: # Receive complete
        break
    listData.append(content)
    # wf.write(content)
    # wf.flush()

# bytes are converted to strings
strData = b"".join(listData).decode("utf-8")
# print(strData)
# wf.close()

# close the connection
clientSocket.close()

# Separate the response header from the HTML content, with parameter 2 indicating only one split
head,htmlbody = strData.split("\r\n\r\n"1),# print(head)
print(htmlbody)Copy the code

The first is on the server side: we use bind() to give the socket a fixed address and port, and listen() to passively listen for that port. When a client attempts to connect using the connect() method, the server accepts the connection using accept(), thus establishing a socket for the connection:

import socket

# Address
HOST = ' '
PORT = 8000

reply = 'Yes'

Create socket, bind socket to local IP and port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
# passively wait, 3: maximum number of connections in the queue
s.listen(3)

# accept and establish connectionconn, 
addr = s.accept()

# receive message
request = conn.recv(1024)
print 'request is: ',request
print 'Connected by', addr

# send message
conn.sendall(reply)

# close connection
conn.close()
Copy the code

Save the above as a file org_ser.py

Second, the client side: We actively use the connect() method to search for the IP address and port on the server side so that the client can find the server and establish a connection.

import socket

# Address
HOST = '172.20.202.155'
PORT = 8000

request = 'can you hear me? '

# configure socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

# send message
s.sendall(request)

# receive message
reply = s.recv(1024)
print 'reply is: ',reply

# close connection
s.close()
Copy the code

Debug run:

Request = ‘Can you hear me?’ Request = ‘Can you hear me? ‘, and the client gets “Yes”.

Program running fine

Author: rebirth_2017 links: www.jianshu.com/p/a324616b8… Brief book copyright belongs to the author, any form of reprint please contact the author to obtain authorization and indicate the source.