3
#!/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\n内容-处置:表单-数据;name="a"\r\n\r\n上传\r\n-----------------------------15758382912173403811539561297\r\ nContent-Disposition:表单数据;名称="文件[]"; filename="Datos_Pegados_0a17.jpg"\r\n内容类型:图像/jpeg\r\n\r\nÿØÿà

这就是我使用篡改数据得到的结果。

postfield的有趣部分:

表格数据;name="a"\r\n\r\n上传\r\n

表格数据;名称="文件[]"

所以...你说 POST_DATA 应该是 'a=upload&file[]=FILE'?

更新2:

<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&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
4

3 回答 3

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。

于 2011-02-09T09:55:40.220 回答
2

Error 26 (in "pycurl.error: (26, 'failed creating formpost data')") means that the filename you specified to upload does not exist. I was running into this same error, and that was definitely the problem. See the curl source code for the location that it generates error 26.

于 2011-08-12T06:50:18.670 回答
1

我认为 for 的参数POSTFIELDS需要是一个简单的 URL 编码字符串,例如:

POST_DATA = 'a=foo&b=bar'

接下来,我不确定您的 FILE 内容。查看此邮件以获取示例。

于 2009-03-25T05:17:28.737 回答