使用python发送HTTP请求

时间:2014-05-17 12:19:14

标签: python http-post fiddler httplib

我使用FIDDLER截获了一个HTTP请求。

我想发送相同的请求,这次是使用python而不是我的浏览器。

我做了一些研究,发现httplib是要走的路,但由于某些原因,我的代码不起作用。

我的计算机没有发送任何请求(我可以使用FIDDLER验证这一点)。

任何想法都错了吗?

import httplib

headers = {
"Host":" unive.edu",
"Connection":" keep-alive",
"Content-Length":" 5142",
"Cache-Control":" max-age=0",
"Accept":" text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Origin":" http://unive.edu",
"User-Agent":" Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36",
"Content-Type":" application/x-www-form-urlencoded",
"DNT":" 1",
"Referer":" http://unive.edu/webtixsnetglilot/SelectCoursePage2.aspx?dtticks=635358672778578750&hideBackButton=1",
"Accept-Encoding":" gzip,deflate,sdch",
"Accept-Language":" en-US,en;q=0.8,he;q=0.6",
"Cookie":" ASP.NET_SessionId=c5nch3ouzxkaaa5bk1rflaic; __atuvc=1%7C16%2C1%7C17%2C0%7C18%2C0%7C19%2C1%7C20; __utma=184162612.90089091.1384326557.1400242958.1400259831.35; __utmb=184162612.4.10.1400259831; __utmc=184162612; __utmz=184162612.1384326557.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); SS#177904#2#1#StopLoad#635358666509203750=true; SS#177904#2#1#StopLoad#635358672778891250=true; hfSKey=|||||||||gli; hfOIKey=imVrnYvw; SS#177904#2#1#StopLoad#635358677931547500=true;"
}

body = ""

conn = httplib.HTTPConnection("unive.edu")
conn.request("POST", "/web/SelectCoursePage2.aspx?dtticks=635358682524828750&hideBackButton=1", None, headers)
res = conn.getresponse()

谢谢,迈克尔。

2 个答案:

答案 0 :(得分:1)

问题是,您有一个Content-Length标头声称您要在5142机构中发送服务器POST字节,但是您已经#39} ;根本不发送任何身体内容。结果,服务器挂起等待身体;它可能会在几分钟内耗尽时失败。

要解决此问题,请确保Content-Length标头符合正文的长度(以字节为单位)。

答案 1 :(得分:0)

请尝试print(res.status)

否则我正在使用的是:http://docs.python-requests.org/en/latest/