简体   繁体   English

pycurl PostFields选项的用法

[英]pycurl PostFields option usage

I'm trying to use pycurl to upload a file to Processmaker. 我正在尝试使用pycurl将文件上传到Processmaker。 app, self.usr, and doc are strings. app,self.usr和doc是字符串。 file is a django file field object. file是django文件字段对象。 I'm currently just passing the object. 我目前正在传递对象。 I'm fairly sure I'm just passing the incorrect object/type/thing to the ATTACH_FILE field. 我相当确定我只是将错误的对象/类型/内容传递给ATTACH_FILE字段。

The working php POSTFIELDS definition looks like this: 可用的php POSTFIELDS定义如下:

$params = array (
'ATTACH_FILE'  => '@/home/test.txt',
'APPLICATION'  => $resultCase->caseId,
'INDEX'        => 1,
'USR_UID'      => $oRandomUser->guid,
'DOC_UID'      => '3154812864d55a6e017ff65089604572',
'APP_DOC_TYPE' => 'INPUT',
'TITLE'        => "Initial document".date("Y-m-d H:i:s"),
'COMMENT'      => "this document was uploaded by the system"

curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

And the currently broken python: 和当前损坏的python:

    c = pycurl.Curl()
    data = [
            ('ATTACH_FILE', (pycurl.READFUNCTION, file.read)),
            ('APPLICATION', app),  
            ('INDEX' , 1),
            ('USR_UID', self.usr),
            ('DOC_UID', doc),
            ('APP_DOC_TYPE', 'INPUT')
           ]

    post = urllib.urlencode(data)

    print post

    url = "http://192.168.51.155/sysworkflow/en/green/services/upload"

    c.setopt(pycurl.URL, url)
    c.setopt(pycurl.VERBOSE, 1)
    c.setopt(pycurl.POST, 1)
    c.setopt(pycurl.POSTFIELDS, post)

    c.perform()

    c.close()

Any ideas? 有任何想法吗?

I found a way to solve my own issue. 我找到了解决自己问题的方法。 Here is what I did, using poster located here: http://atlee.ca/software/poster/ I did the following: 这是我使用位于此处的海报进行的操作: http : //atlee.ca/software/poster/我做了以下操作:

from poster.streaminghttp import register_openers
import poster

register_openers()

url = "http://192.168.51.155/sysworkflow/en/green/services/upload"

params = { 
'APPLICATION' : app,  
'INDEX' : 1,
'USR_UID' : self.usr,
'DOC_UID' : doc,
'APP_DOC_TYPE' : 'INPUT',
'TITLE' : 'Test',
'ATTACH_FILE' : open(file.path, "rb")
}

datagen, headers = poster.encode.multipart_encode(params)
request = urllib2.Request(url, datagen, headers)
result = urllib2.urlopen(request)
print result.read()

Much easier to use than pycurl! 比pycurl更容易使用! The problem with my first attempt was that POSTFIELDS can't accept files (without some wrangling) and using an HTTPPOST option would work with the files but was difficult to get working with both file data and field data. 我第一次尝试的问题是POSTFIELDS无法接受文件(无需费劲),并且使用HTTPPOST选项可以处理这些文件,但是很难同时处理文件数据和字段数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM