无法使用urllib和urllib2发送数据(python) [英] unable to send data using urllib and urllib2 (python)
问题描述
大家好(首先在这里发帖)。
我试图发送数据到一个网页。如果一切正常,这个网页要求两个字段(一个文件和一个电子邮件地址),网页返回一个页面,说一切正常,并发送一个文件到提供的电子邮件地址。我执行下面的代码,我的电子邮件帐户里什么也没有。
pre $ ur $ b $ ur $ $
$ b params = urllib.urlencode({'uploaded':open('file'),'email':'user@domain.com'))
req = urllib2.urlopen('http:// webpage。打印命令为我提供了一个简单的例子,我认为(基于谷歌搜索)的海报模块(我认为应该给一切正常页面的代码)。
应该做的伎俩,但我需要保持依赖到最低限度,因此我想要一个解决方案使用标准库(如果可能的话)。
在此先感谢。 / p>
感谢大家的回答。
$ p $ import mechanize
br = mechanize.Browser()
br.open('webpage.com')
email='user@domain.com'
br.select_form(nr = 0)
br ['email'] = email
br.form.add_file(打开('filename'),'mime-type','filename')
br.form.set_all_readonly(False)
br.submit()
Hello everybody (first post here).
I am trying to send data to a webpage. This webpage request two fields (a file and an e-mail address) if everything is ok the webpage returns a page saying "everything is ok" and sends a file to the provided e-mail address. I execute the code below and I get nothing in my e-mail account.
import urllib, urllib2
params = urllib.urlencode({'uploaded': open('file'),'email': 'user@domain.com'})
req = urllib2.urlopen('http://webpage.com', params)
print req.read()
the print command gives me the code of the home page (I assume instead it should give the code of the "everything is ok" page).
I think (based o google search) the poster module should do the trick but I need to keep dependencies to a minimum, hence I would like a solution using standard libraries (if that is possible).
Thanks in advance.
Thanks everybody for your answers. I solve my problem using the mechanize library.
import mechanize
br = mechanize.Browser()
br.open('webpage.com')
email='user@domain.com'
br.select_form(nr=0)
br['email'] = email
br.form.add_file(open('filename'), 'mime-type', 'filename')
br.form.set_all_readonly(False)
br.submit()
这篇关于无法使用urllib和urllib2发送数据(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!