使用python发送HTTP请求 [英] Sending a HTTP request using python

查看:132
本文介绍了使用python发送HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用FIDDLER拦截了HTTP请求.

I have intercepted an HTTP request using FIDDLER.

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

I would like to send the same request, just this time using python and not my browser.

我做了一些研究,发现httplib是可行的方法,但是由于某种原因,我的代码无法正常工作.

I did some research and found that httplib is the way to go, but for some reason my code does not work.

我的计算机没有发送任何请求(我可以使用FIDDLER进行验证).

No request is sent from my computer (I can verify this by using 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()

谢谢迈克尔.

推荐答案

问题是您有一个 Content-Length 标头,声称您要发送服务器 POST 正文中的5142 个字节,但您根本没有发送任何正文内容.结果,服务器挂起等待主体;几分钟后可能会失败.

The problem is that you've got a Content-Length header claiming that you're going to send the server 5142 bytes in the POST body but you're not sending any body content at all. As a consequence, the server hangs waiting for the body; it will probably fail with a time out in a few minutes.

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

To fix this, ensure that the Content-Length header matches the length of the body in bytes.

这篇关于使用python发送HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆