【python socket编程】—— 2.解析http请求头

前文:【python socket编程】—— 1.初探

在上一篇文章中我们知道,socket.accept()接受的数据是请求头,请求头格式是这样的:

POST /login HTTP/1.1
Host: 127.0.0.1:1207
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://127.0.0.1:1207/login
Content-Type: application/x-www-form-urlencoded
Content-Length: 27
Cookie: D9zb2HfPUf8RI7AOwsUZpiBq
Connection: keep-alive
Upgrade-Insecure-Requests: 1

username=Harp&password=1234

这是一个POST请求,请求头第一行是POST /login HTTP/1.1,以空格划分,分别是请求方法、路径和协议,之后则是key: value形式的各种数据组成的header,我们暂且不管每一行分别是什么意思,然后是空行,结尾的username=Harp&password=1234body部分,表单通过POST方法提交的数据(如账号密码)就在这里,GET请求则没有body部分。请求头的换行使用的是'/r/n'

编写一个Request类,来解析请求的方法、路径、headerbody,如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# contact: liutao25@baidu.com

from urllib.parse import unquote, quote


class Request:
    def __init__(self, r):
        self.content = r
        self.method = r.split()[0]
        self.path = r.split()[1]
        self.body = r.split('\r\n\r\n', 1)[1]

    def form_body(self):
        return self._parse_parameter(self.body)

    def parse_path(self):
        index = self.path.find('?')
        if index == -1:
            return self.path, {}
        else:
            path, query_string = self.path.split('?', 1)
            query = self._parse_parameter(query_string)
            return path, query

    @property
    def headers(self):
        header_content = self.content.split('\r\n\r\n', 1)[0].split('\r\n')[1:]
        result = {}
        for line in header_content:
            k, v = line.split(': ')
            result[quote(k)] = quote(v)
        return result

    @staticmethod
    def _parse_parameter(parameters):
        args = parameters.split('&')
        query = {}
        for arg in args:
            k, v = arg.split('=')
            query[k] = unquote(v)
        return query

下一篇文章:【python socket编程】—— 3.响应

    原文作者:HTTP
    原文地址: https://segmentfault.com/a/1190000013031253
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞