pycurl PostFields 选项用法

发布于 2024-10-17 20:26:50 字数 1257 浏览 4 评论 0原文

我正在尝试使用 pycurl 将文件上传到 Processmaker。 app、self.usr 和 doc 是字符串。 file 是 django 文件字段对象。我目前只是传递该对象。我相当确定我只是将错误的对象/类型/事物传递给 ATTACH_FILE 字段。

工作的 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);

当前损坏的 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()

有什么想法吗?

I'm trying to use pycurl to upload a file to Processmaker. app, self.usr, and doc are strings. file is a django file field object. 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.

The working php POSTFIELDS definition looks like this:

$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:

    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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-10-24 20:26:50

我找到了解决我自己问题的方法。这是我所做的,使用位于此处的海报: 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()

比 pycurl 更容易使用!我第一次尝试的问题是 POSTFIELDS 无法接受文件(没有一些争论)并且使用 HTTPPOST 选项可以处理文件,但很难同时处理文件数据和字段数据。

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:

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! 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文