#!/usr/bin/python
import pycurl
import re
import StringIO
#CONSTANTS
URL = "http://www.imagehost.org"
FILE = "/datos/poop1.jpg"
POST_DATA = [("a", "upload"), ("file[]", (pycurl.FORM_FILE, FILE))]
buffer = StringIO.StringIO()
c = pycurl.Curl()
c.setopt( c.URL, URL )
c.setopt( c.POST, 1 )
c.setopt( c.POSTFIELDS, POST_DATA )
##c.setopt( c.HTTPPOST, POST_DATA )
c.setopt( c.USERAGENT,'Curl')
c.setopt( c.WRITEFUNCTION, buffer.write)
c.setopt(pycurl.VERBOSE, 1)
c.perform()
c.close()
#c.setopt(c.PROXY, proxyHostAndPort)
#c.setopt(c.PROXYUSERPWD, proxyAuthentication)
parse = buffer.getvalue()
pattern = re.compile('/<td nowrap="nowrap">(.+)<\/td>\s*<td class="link"><input.+value="([^"]+)" \/><\/td>/i')
result = re.search(pattern, parse)
print result
问题在于如何做这个帖子。
c.setopt(c.POSTFIELDS,POST_DATA)不接受列表,那么我应该怎么做而不是添加列表?
c.setopt(c.HTTPPOST,POST_DATA)掉线:
Traceback (most recent call last):
File "pymage", line 26, in <module>
c.perform() pycurl.error: (26, 'failed creating formpost data')
更新
----------------------------- 15758382912173403811539561297 \ r \ nConContent-Disposition:form-data; NAME = “一个为” \ r \ n \ r \ nupload \ r \ n ----------------------------- 15758382912173403811539561297 \ r \ nContent-Disposition:form-data; NAME = “文件[]”; filename =“Datos_Pegados_0a17.jpg”\ r \ nConContent-Type:image / jpeg \ r \ n \ r \nÿÿÿà
这就是我使用篡改数据。
战场的有趣部分:
形式数据; NAME = “一个为” \ r \ n \ r \ nupload \ r \ n
形式数据; NAME = “文件[]”
所以......你说POST_DATA应该是'a = upload&amp; file [] = FILE'?
UPDATE2:
<form method="post" action="/" enctype="multipart/form-data" onsubmit="javascript:Upload(); return true;">
<input type="hidden" name="a" value="upload" />
<td class="left">File:</td>
td class="right"><input name="file[]" type="file" size="20" /></td>
这就是代码......
现在它正在使用表单数据配置,但它不会上传我认为的文件
c.setopt(c.POSTFIELDS,'a = upload&amp; file [] = / datos / poop1.jpg')
我得到了这个:
* About to connect() to www.imagehost.org port 80 (#0)
* Trying 74.63.87.74... * connected
* Connected to www.imagehost.org (74.63.87.74) port 80 (#0)
> POST / HTTP/1.1
User-Agent: Curl
Host: www.imagehost.org
Accept: */*
Content-Length: 32
Content-Type: application/x-www-form-urlencoded
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Date: Wed, 25 Mar 2009 06:53:49 GMT
< Content-Type: text/html
< Server: nginx/0.7.11
< Set-Cookie: userhash=7c09b97cc70c8c133c850a3e744b416e; expires=Thu, 25-Mar-2010 06:53:49 GMT; path=/; domain=.imagehost.org; httponly
<
* Connection #0 to host www.imagehost.org left intact
* Closing connection #0
答案 0 :(得分:6)
pycurl文档在这方面并不是那么清楚,但HTTPPOST选项可以获取元组列表,每个元素长度恰好是两个元素。每个元组的第一个参数是表单字段名称,第二个参数是值。
但是,该值也可能是元组。此元组必须包含与该字段相关的数据对:(form_option,option_value,form_option,option_value等等)
例如,包含三个字段的多部分表单,最后一个是文件上传 - 我们可以设置文件名和mime类型:
c = pycurl.Curl()
c.setopt(c.URL, base_url + 'upload.cgi')
c.setopt(c.HTTPPOST,[ ("fieldname1", "value1"),
("fieldname2", "value2"),
("uploadfieldname",
(c.FORM_FILE, local_filename,
c.FORM_CONTENTTYPE, "application/x-gzip"))
])
您可以在curl_formadd()的C API文档中找到选项: http://curl.haxx.se/libcurl/c/curl_formadd.html 但是,从pycurl源看,只支持FORM_FILE,FORM_FILENAME,FORM_CONTENTTYPE和FORM_COPYCONTENTS。
答案 1 :(得分:1)
我认为POSTFIELDS
的参数需要是一个简单的URL编码字符串,例如:
POST_DATA = 'a=foo&b=bar'
接下来,我不确定你的FILE内容。查看this mail message以获取示例。
答案 2 :(得分:1)
错误26(在“pycurl.error:(26,'创建formpost数据失败')中”)表示您指定要上传的文件名不存在。我遇到了同样的错误,这肯定是问题所在。请参阅curl源代码,了解它生成的位置错误26。