【问题标题】:Sending a HTTP request using python使用 python 发送 HTTP 请求
【发布时间】:2023-11-08 21:25:01
【问题描述】:

我已经使用 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()

谢谢,迈克尔。

【问题讨论】:

    标签: python http-post fiddler httplib


    【解决方案1】:

    问题是您有一个Content-Length 标头,声称您将在POST 正文中向服务器发送5142 字节,但您根本没有发送任何正文内容。结果,服务器挂起等待正文;它可能会在几分钟后超时并失败。

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

    【讨论】:

      【解决方案2】:

      请尝试print(res.status)

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

      【讨论】:

      • 它实际上卡在“res = conn.getresponse()”上,之后它永远不会到达命令