Discuss / Python / 求解,我的代码为什么请求失败?

求解,我的代码为什么请求失败?

Topic source

coding=utf-8

import socket

#创建一个socket s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

#建立连接 s.connect(('www.sina.com.cn',80))

#发送数据 s.send(b'GET/HTTP/1.1\r\nHost: www.sina.com.cn\r\nConnection:close\r\n\r\n')

#接收数据 buffer = [] while True:

#每次最多接收1k字节
d = s.recv(1024)
if d:
    buffer.append(d)
else:
    break

data = b''.join(buffer) s.close()

#将接收到的数据的http头和网页分离 header,html = data.split(b'\r\n\r\n',1) print(header.decode('utf-8'))

#把接收的数据写入文件 with open('/Users/win10/Desktop/sina.html','wb') as f: f.write(html)

打印的头:
HTTP/1.1 400 Bad Request Server: nginx Date: Thu, 26 Apr 2018 03:27:09 GMT Content-Type: text/html Content-Length: 166 Connection: close X-Via-CDN: f=edge,s=cmcc.hangzhou.ha2ts4.75.nb.sinaedge.com,c=112.17.64.45;


  • 1

Reply