【问题标题】:When I user poster to post a file to the url, An error has occurred当我使用海报将文件发布到 url 时,发生错误
【发布时间】:2014-09-14 06:57:33
【问题描述】:

这是我的代码:

user_agent = *Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)*

#######cookie

opener = poster.streaminghttp.register_openers()

opener.add_handler(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))

params={*zipFile*:open("123456.zip","rb"),"projectName":"test"}

datagen, headers = poster.encode.multipart_encode(params)

reqIdea = urllib2.Request("http://biqatest.baidu.com/dispatch/project/upload", datagen, headers)

reqIdea.add_header(*User-Agent*,user_agent)

print urllib2.urlopen(reqIdea).read()

然后出现错误:

File "C:\Python27\lib\urllib2.py", line 382, in _call_chain

result = func(*args)

File "C:\Python27\lib\urllib2.py", line 531, in http_error_default

raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

urllib2.HTTPError: HTTP Error 405: Method Not Allowed

在我上传文件之前,我必须登录。

所以我使用 cookie。

省略了一些代码。

没关系。

【问题讨论】:

    标签: python poster


    【解决方案1】:

    我猜您正在尝试执行 POST 请求。为此,您需要适当地设置get_method。简单设置:

    reqIdea.get_method = lambda: "POST"
    

    我还建议您为此类事情使用更好的第三方库。 (如requests

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多