#!/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