python - urrlib2 请求 https 站点 - 收到 400 错误
使用以下代码片段访问带有帖子的 url。
我可以使用 wget 和以下命令获取它: wget --post-data 'p_calling_proc=bwckschd.p_disp_dyn_sched&p_term=201010' https:// /spectrumssb2.memphis.edu/pls/PROD/bwckgens.p_proc_term_date
由于某种原因,我的 python 文本出现问题,错误代码为 400。(当然,浏览器的工作方式为预期)
任何想法/评论/等等...
我的Python测试:
//================================ ============
import urllib
import urllib2
import sys, string
import time
import mechanize
Request = urllib2.Request
urlopen = urllib2.urlopen
headers ={'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
query = "p_calling_proc%3Dbwckschd.p_disp_dyn_sched%26p_term%3D201010"
url1="https://spectrumssb2.memphis.edu/pls/PROD/bwckgens.p_proc_term_date"
req = Request(url1, query, headers)
test1=0
test=0
while test==0:
print "aaaaattttt \n"
try:
res = urlopen(req)
#req = Request(url1, query, headers)
print "aaaappppp \n"
#urllib2.URLError, (e)
#print e
except urllib2.HTTPError, e:
print "ffff1111 "+str(e.code)+"\n"
if e.code:
test1=1
print "error ..sleep \n"
time.sleep(1)
else:
test1=0
except urllib2.URLError, e:
print e.reason
#print "ffff3333 "+e.code+"\n"
if e.reason:
test1=1
print "error ..sleep \n"
time.sleep(1)
else:
test1=0
#print "ddd "+e.code +"\n"
#print e
if test1==0:
test=1
print "test1 = "+str(test1)+"\n"
#res = urlopen(req)
print "gggg 000000000000\n"
s = res.read()
。
任何想法/评论将不胜感激..
谢谢
using the following snip of code to access a url with a post.
i can get it using wget and the following:
wget --post-data 'p_calling_proc=bwckschd.p_disp_dyn_sched&p_term=201010' https://spectrumssb2.memphis.edu/pls/PROD/bwckgens.p_proc_term_date
for some reason, i'm having an issue with my python text, in that i get a errorcode of 400. (and of course the browser works as expected)
any thoughts/comments/etc...
the python test that i have:
//==========================================
import urllib
import urllib2
import sys, string
import time
import mechanize
Request = urllib2.Request
urlopen = urllib2.urlopen
headers ={'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
query = "p_calling_proc%3Dbwckschd.p_disp_dyn_sched%26p_term%3D201010"
url1="https://spectrumssb2.memphis.edu/pls/PROD/bwckgens.p_proc_term_date"
req = Request(url1, query, headers)
test1=0
test=0
while test==0:
print "aaaaattttt \n"
try:
res = urlopen(req)
#req = Request(url1, query, headers)
print "aaaappppp \n"
#urllib2.URLError, (e)
#print e
except urllib2.HTTPError, e:
print "ffff1111 "+str(e.code)+"\n"
if e.code:
test1=1
print "error ..sleep \n"
time.sleep(1)
else:
test1=0
except urllib2.URLError, e:
print e.reason
#print "ffff3333 "+e.code+"\n"
if e.reason:
test1=1
print "error ..sleep \n"
time.sleep(1)
else:
test1=0
#print "ddd "+e.code +"\n"
#print e
if test1==0:
test=1
print "test1 = "+str(test1)+"\n"
#res = urlopen(req)
print "gggg 000000000000\n"
s = res.read()
.
any thoughts/comments would be appreciated..
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试不对查询字符串进行编码。 POST 数据中的 & 和 = 不需要是 urlencoded。如果远程端的 Web 应用程序不需要查询字符串中的 %xx 编码,则它将无法解析它。
这是curl 的HTTP 请求标头:
这是来自Python 的HTTP 请求标头:
Try not encoding the query string. The &'s and ='s in the POST data don't need to be urlencoded. If the web app on the remote end does not expect the %xx encoding in the query string, it won't be able to parse it.
Here's curl's HTTP request headers:
And here's the HTTP request headers from your python:
我认为您的查询字符串不太正确。尝试使用 urllib.urlencode() 方法生成查询,a la
I think your query string is not quite right. Try using the urllib.urlencode() method to generate the query, a la