gpt4 book ai didi

python - urrlib2 请求 https 站点 - 出现 400 错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:35 25 4
gpt4 key购买 nike

使用以下代码片段访问带有帖子的 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()

.


任何想法/意见将不胜感激..

谢谢

最佳答案

尽量不要对查询字符串进行编码。 POST 数据中的 & 和 = 不需要是 urlencoded .如果远程端的 Web 应用程序不希望查询字符串中有 %xx 编码,它将无法解析它。

这是 curl 的 HTTP 请求 header :

POST / HTTP/1.1
User-Agent: curl/7.19.4 (universal-apple-darwin10.0) libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3
Host: 127.0.0.1
Accept: */*
Content-Length: 188
Expect: 100-continue

bwckschd.p_disp_dyn_sched&p_term=201010

这是来自您的 python 的 HTTP 请求 header :

POST / HTTP/1.1
Accept-Encoding: identity
Content-Length: 60
Host: 127.0.0.1
Content-Type: application/x-www-form-urlencoded
Connection: close
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)

p_calling_proc%3Dbwckschd.p_disp_dyn_sched%26p_term%3D201010

关于python - urrlib2 请求 https 站点 - 出现 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1934284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com