In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how Python socket interprets the content of HTTP requests. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Python socket interprets the content of HTTP requests.
Socket parses the content of HTTP request. 1. Parse the header of the HTTP request
The Terminator line of the HTTP request header is "\ r\ n", and you can read the contents of the HTTP request header by line. If you read the behavior "\ r\ n", the HTTP request header ends.
two。 The request header contains Content-Length parameters
If there is a Content-Length parameter in the HTTP request, the content size of the HTTP request is determined. The request can read the value of Content-Length directly, and then read the contents of the corresponding bytes.
3. The request header contains the parameter Transfer-Encoding: chunked
If there is a Transfer-Encoding parameter in the HTTP request, the content size of the HTTP request is uncertain, and the Terminator of this content is "0\ r\ n\ r\ n", so you can read the content part of the HTTP request by line. If you read "0\ r\ n" and "\ r\ n" in succession, the content has been read.
Code implementation
In the code: self._file stands for socket.makefile ()
Def get_http_content (self): content_length = 0 transfer_encoding = False while True: req_line = self._file.readline () req_line = str (req_line "utf-8") # encountered http header Terminator # read http content if req_line = = "\ r\ n": if content_length! = 0: content = self._file.read (content_length) content = str (content) "utf-8") self._content = content return None if transfer_encoding: content = "self._file.readline () while True: line = self._file.readline () line = str (line "utf-8") if line = "0\ r\ n": sub_line = self._file.readline () sub_line = str (sub_line "utf-8") if sub_line = = "\ r\ n": self._content = content return None else: content + = line continue Self._content = False # header file does not end # and no field about content size was found else: if content_length = = 0 and transfer_encoding is False: words = req_line.split () if words [0] = = "Content-Length:": Content_length = int (words [1]) if words [0] = "Transfer-Encoding:": transfer_encoding = True self._content = Falsesocket Simulation http request # coding: utf-8import socketfrom urllib.parse import urlparsedef get_url (url): url = urlparse (url) host = url.netloc path = url.path If path = "": path = "/" # establish a socket connection client = socket.socket (socket.AF_INET Socket.SOCK_STREAM) client.connect ((host, 80)) client.send ("GET {} HTTP/1.1\ r\ nHost: {}\ r\ nConnection:close\ r\ n\ n" .format (path Host) .encode ("utf-8") data = b "" while True: d = client.recv (1024) if d: data + = d else: break data = data.decode ("utf-8") html_data = data.split ("\ r\ n\ r\ n") [1] print (html_data) client.close () passif _ _ name__ = ='_ _ main__': get_url ("http://www.baidu.com") so far I believe you have a deeper understanding of "how Python socket interprets the content of HTTP requests", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.