Python經過socket發送http請求

咱們以訪問百度主頁爲案例,使用socket發送http請求。html

import socket
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('www.baidu.com',80))
s.send('''GET https://www.baidu.com/ HTTP/1.1
Host: www.baidu.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Accept-Language: zh-CN,zh;q=0.8

''')

buf=s.recv(1024)
while len(buf):
    print buf
    buf = s.recv(1024)

基於socket進行http編程在於請求參數的可控性更好,可是難度相應就更大了。以上發送的數據是直接從fiddler抓包器裏面複製的。web

相關文章
相關標籤/搜索