在bestream中通过curl发送文件

时间:2015-07-30 02:37:52

标签: curl

我需要像这样的文件上传表格

<form action="http://bs23d2.bestream.tv/core/page/ajax/file_upload_handler.ajax.php?r=bestream.tv&p=http&csaKey1=943c33c68cb53ad6472b7ed02e7fe6b3ece640d6b11e6a5aa44a3a4e1f97e171&csaKey2=5c8b36236ee632ae4a111d4b29bb4dab743d46eaffbdb8e19f60cb5d6a92b916" method="POST" enctype="multipart/form-data">
<input type="file" name="files[]">
<input type="submit" value='Enviar'>
</form>

但在Curl。

使用脚本:

curl -c cookies.txt -d "loginUsername=myuser&loginPassword=mypass&submitme=1" "http://bestream.tv/login.html" 


curl -b cookies.txt -F "files[]=@menu.avi" "http://bs23d2.bestream.tv/core/page/ajax/file_upload_handler.ajax.php?r=bestream.tv&p=http&csaKey1=943c33c68cb53ad6472b7ed02e7fe6b3ece640d6b11e6a5aa44a3a4e1f97e171&csaKey2=5c8b36236ee632ae4a111d4b29bb4dab743d46eaffbdb8e19f60cb5d6a92b916" 

它没有用。

我得到了结果:

[{"size":0,"type":"","name":"Unavailable.","error":"Uploading has been disabled.
","error_result_html":"<td class=\"cancel\">   <img src=\"http:\/\/bestream.tv\/
themes\/blue_v2\/images\/red_error_small.png\" height=\"16\" width=\"16\" alt=\"
error\"\/><\/td><td class=\"name\">Unavailable.<\/td><td class=\"error\" colspan
=\"2\">Error: Uploading has been disabled.<\/td>"}]

会出现什么问题?

1 个答案:

答案 0 :(得分:0)

解答: 通过添加以下内容解决了该问题:

-e "http://bestream.tv/account_home.html"

示例:

curl -e "http://bestream.tv/account_home.html" -b cookies.txt -F "files[]=@menu.avi" "http://bs23d2.bestream.tv/core/page/ajax/file_upload_handler.ajax.php?r=bestream.tv&p=http&csaKey1=943c33c68cb53ad6472b7ed02e7fe6b3ece640d6b11e6a5aa44a3a4e1f97e171&csaKey2=5c8b36236ee632ae4a111d4b29bb4dab743d46eaffbdb8e19f60cb5d6a92b916"

在pycurl中:

import cStringIO
import pycurl
import re
import sys
from humanize import naturalsize
import time

START_TIME = None

def progress(download_t, download_d, upload_t, upload_d):
    if int(upload_t) == 0:
        return
    global START_TIME
    if START_TIME is None:
        START_TIME = time.time()
    duration = time.time() - START_TIME + 1
    speed = upload_d / duration
    speed_s = naturalsize(speed, binary=True)
    speed_s += '/s'
    if int(upload_d) == 0:
        upload_d == 0.01

    p = '%s/%s (%.2f%%) %s %s\r' % (naturalsize(upload_d, binary=True),
                                    naturalsize(upload_t, binary=True),
                                    upload_d / upload_t, speed_s, ' ' * 10)
    sys.stderr.write(p)
    sys.stderr.flush()

response = cStringIO.StringIO()
url = cStringIO.StringIO()

c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://bestream.tv/login.html')
c.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0')
c.setopt(pycurl.POSTFIELDS, 'loginUsername=my_user&loginPassword=my_pass&submitme=1')
c.setopt(pycurl.COOKIEJAR, 'cookie2.txt')
c.perform()
c.close()

c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://bestream.tv/account_home.html')
c.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0')
c.setopt(pycurl.COOKIEFILE, 'cookie2.txt')
c.setopt(pycurl.WRITEFUNCTION, url.write)
c.perform()
c.close()

url = re.search('url: \'(http:\/\/.*)?\'', url.getvalue()).group(1)

c = pycurl.Curl()
c.setopt(pycurl.COOKIEFILE, 'cookie2.txt')
c.setopt(pycurl.URL, url)
c.setopt(pycurl.REFERER, 'http://bestream.tv/account_home.html')
c.setopt(pycurl.USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0')
c.setopt(pycurl.HTTPPOST, [("files[]", (pycurl.FORM_FILE, "menu.avi"))])
c.setopt(pycurl.NOPROGRESS, 0)
c.setopt(pycurl.PROGRESSFUNCTION, progress)
c.setopt(pycurl.WRITEFUNCTION, response.write)

c.perform()
c.close() 
print response.getvalue()

我注意到,在pycurl中比curl慢得多。