Python urllib2 文件上传问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/407468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:01:58  来源:igfitidea点击:

Python urllib2 file upload problems

pythonpostuploadurllib2

提问by Salty

I'm currently trying to initiate a file upload with urllib2 and the urllib2_filelibrary. Here's my code:

我目前正在尝试使用 urllib2 和urllib2_file库启动文件上传。这是我的代码:

import sys
import urllib2_file
import urllib2

URL='http://aquate.us/upload.php'
d = [('uploaded', open(sys.argv[1:]))]
req = urllib2.Request(URL, d)
u = urllib2.urlopen(req)
print u.read()

I've placed this .py file in my My Documents directory and placed a shortcut to it in my Send To folder (the shortcut URL is ).

我已经将这个 .py 文件放在我的文档目录中,并在我的发送到文件夹中放置了它的快捷方式(快捷方式 URL 是 )。

When I right click a file, choose Send To, and select Aquate (my python), it opens a command prompt for a split second and then closes it. Nothing gets uploaded.

当我右键单击一个文件,选择发送到,然后选择 Aquate(我的 python)时,它会打开一个命令提示符一秒钟,然后关闭它。什么都不会上传。

I knew there was probably an error going on so I typed the code into CL python, line by line. When I ran the u=urllib2.urlopen(req)line, I didn't get an error; alt text http://www.aquate.us/u/55245858877937182052.jpg

我知道可能发生了错误,所以我将代码逐行输入到 CL python 中。当我运行u=urllib2.urlopen(req)线路时,我没有收到错误消息; 替代文字 http://www.aquate.us/u/55245858877937182052.jpg

instead, the cursor simply started blinking on a new line beneath that line. I waited a couple of minutes to see if something would happen but it just stayed like that. To get it to stop, I had to press ctrl+break.

相反,光标只是开始在该行下方的新行上闪烁。我等了几分钟想看看是否会发生什么,但它就一直这样。为了让它停止,我不得不按 ctrl+break。

What's up with this script?

这个脚本是怎么回事?

Thanks in advance!

提前致谢!

[Edit] Forgot to mention -- when I ran the script without the request data (the file) it ran like a charm. Is it a problem with urllib2_file?

[编辑] 忘了提——当我在没有请求数据(文件)的情况下运行脚本时,它运行起来就像一个魅力。是 urllib2_file 的问题吗?

[edit 2]:

[编辑2]:

import MultipartPostHandler, urllib2, cookielib,sys
import win32clipboard as w
cookies = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),MultipartPostHandler.MultipartPostHandler)
params = {"uploaded" : open("c:/cfoot.js") }
a=opener.open("http://www.aquate.us/upload.php", params)
text = a.read()
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardText(text)
w.CloseClipboard()

That code works like a charm if you run it through the command line.

如果您通过命令行运行该代码,那么它就像一个魅力。

回答by Ben Blank

If you're using Python 2.5 or newer, urllib2_fileis both unnecessary and unsupported, so check which version you're using (and perhaps upgrade).

如果您使用的是 Python 2.5 或更高版本,urllib2_file则既不必要又不受支持,因此请检查您使用的是哪个版本(可能还需要升级)。

If you're using Python 2.3 or 2.4 (the only versions supported by urllib2_file), try running the sample codeand see if you have the same problem. If so, there is likely something wrong either with your Python or urllib2_fileinstallation.

如果您使用的是 Python 2.3 或 2.4( 支持的唯一版本urllib2_file),请尝试运行示例代码,看看您是否有同样的问题。如果是这样,则您的 Python 或urllib2_file安装可能有问题。

EDIT:

编辑:

Also, you don't seem to be using either of urllib2_file's two supported formats for POST data. Try using oneof the following two lines instead:

此外,您似乎没有使用urllib2_filePOST 数据的两种受支持格式中的任何一种。尝试使用一个下面两行代替:

d = ['uploaded', open(sys.argv[1:])]
## --OR-- ##
d = {'uploaded': open(sys.argv[1:])}

回答by S.Lott

First, there's a third way to run Python programs.

首先,还有第三种方式来运行 Python 程序。

From cmd.exe, type python myprogram.py. You get a nice log. You don't have to type stuff one line at a time.

在 cmd.exe 中,键入python myprogram.py. 你得到一个不错的日志。您不必一次输入一行内容。

Second, check the urrlib2documentation. You'll need to look at urllib, also.

其次,查看urrlib2文档。您还需要查看urllib

A Request requires a URL and a urlencoded encoded buffer of data.

请求需要一个 URL 和一个经过 urlencoded 编码的数据缓冲区。

data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.

data 应该是标准 application/x-www-form-urlencoded 格式的缓冲区。urllib.urlencode() 函数采用 2 元组的映射或序列,并以这种格式返回一个字符串。

You need to encode your data.

您需要对数据进行编码。

回答by abjennings

If you're still on Python2.5, what worked for me was to download the code here:

如果您仍在使用 Python2.5,那么对我有用的是在此处下载代码:

http://peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html

http://peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html

and save it as MultipartPostHandler.py

并将其保存为 MultipartPostHandler.py

then use:

然后使用:

import urllib2, MultipartPostHandler

opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler())
opener.open(url, {"file":open(...)})

or if you need cookies:

或者如果您需要 cookie:

import urllib2, MultipartPostHandler, cookielib

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), MultipartPostHandler.MultipartPostHandler())
opener.open(url, {"file":open(...)})