Python无法在HTTP套接字服务器中接收用户数据

时间:2017-05-03 21:10:12

标签: python sockets http header recv

我无法获取recv()中的数据 我想要用户在表单字段中输入的数据

代码:

def recvall(self, conn):
    BUFF_SIZE = 4096 # 4 KiB
    data = ""

    while True:

        packet = conn.recv(BUFF_SIZE).decode('utf-8')
        data += packet

        if len(packet) < BUFF_SIZE:
            break

    print (data)

输出:

POST /search HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 14
Cache-Control: max-age=0
Origin: http://localhost
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

似乎缺少一条线或者我尝试过的东西 我想要最后的数据

1 个答案:

答案 0 :(得分:0)

在Steffen正确观察到标题序列后,您已停止阅读。以下代码应该有效(小心,未经测试!):

def recvall(self, conn):
    BUFF_SIZE = 4096

    while True:
        packet = conn.recv(BUFF_SIZE)
        if not packet:
            break
        print packet.decode('utf-8')