Python urllib2 文件上传问题

Python urllib2 file upload problems(Python urllib2 文件上传问题)
本文介绍了Python urllib2 文件上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

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()

我已将此 .py 文件放在我的我的文档"目录中,并在我的发送到"文件夹中放置了一个快捷方式(快捷方式 URL 为 ).

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 ).

当我右键单击一个文件,选择发送到",然后选择Aquate"(我的 Python)时,它会在瞬间打开一个命令提示符,然后将其关闭.什么都没有上传.

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.

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

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

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

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.

这个脚本是怎么回事?

提前致谢!

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

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?

:

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.

推荐答案

如果您使用的是 Python 2.5 或更高版本,urllib2_file 既不必要也不支持,因此请检查您使用的是哪个版本(并可能升级).

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

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

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

此外,您似乎没有使用 urllib2_file 的两种支持的 POST 数据格式.尝试使用以下两行中的 一个:

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

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

这篇关于Python urllib2 文件上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to find element by part of its id name in selenium with python(如何使用python在selenium中通过其id名称的一部分查找元素)
NoSuchElementException: Message: Unable to locate element while trying to click on the button VISA through Selenium and Python(NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法找到元素) - IT屋-程序员软件开发技术分
Selenium not able to click on Get Data button on using Python(Selenium 在使用 Python 时无法单击“获取数据按钮)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python(selenium.common.exceptions.ElementClickInterceptedException:消息:元素点击被拦截:元素不可点击
Selenium Compound class names not permitted(不允许使用硒化合物类名称)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium(selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:尝试使用 selenium 单