Python socket怎么解析HTTP请求内容

发布时间:2022-02-14 09:42:34 作者:iii
来源:亿速云 阅读:383

本篇内容主要讲解“Python socket怎么解析HTTP请求内容”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python socket怎么解析HTTP请求内容”吧!

socket解析HTTP请求内容

思路

1. 解析HTTP请求的头部

HTTP请求头部的结束符行为"\r\n",可以按行读取HTTP请求头的内容,如果读到一行为"\r\n",说明HTTP请求头结束。

2. 请求头里面含有Content-Length参数

如果HTTP请求里面有Content-Length参数,说明HTTP请求的内容大小是确定的,请求直接读取Content-Length的值,然后读取相应字节的的内容即可。

3. 请求头里面含有Transfer-Encoding: chunked 参数

如果HTTP请求里面有Transfer-Encoding参数,说明HTTP请求的内容大小是不确定的,这种内容的结束符是"0\r\n\r\n",因此可以按行读取HTTP请求的内容部分,如果连续读到"0\r\n"和"\r\n"说明内容读取完毕。

代码实现

代码中: self._file 代表的是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")
 
            # 遇到http头结束符
            # 读取http内容
            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
 
            # 头文件没有结束
            # 并且没有找到关于内容大小的字段
            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 = False

socket 模拟http请求

# coding: utf-8
import socket
from urllib.parse import urlparse
def get_url(url):
    url = urlparse(url)
    host = url.netloc
    path = url.path
    if path == "":
        path = "/"
    # 建立 socket 连接
    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\r\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()
    pass
if __name__ == '__main__':
    get_url("http://www.baidu.com")

到此,相信大家对“Python socket怎么解析HTTP请求内容”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. http tcp/ip socket
  2. 如何使用C#中的Socket发送HTTP/HTTPS请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python socket http

上一篇:python中怎么使用requests下载文件

下一篇:如何设置ASP.Net Core MVC开发环境

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》