使用 pyCurl 进行 POST 时出现问题

发布于 2024-10-02 09:57:29 字数 1053 浏览 0 评论 0原文

我正在尝试使用 CURL 将文件发布到网络服务(这是我需要使用的,所以我不能采取扭曲或其他东西)。问题是,当使用 pyCurl 时,网络服务不会收到我发送的文件,如文件底部评论的情况。我的 pyCurl 脚本中做错了什么?有什么想法吗?

非常感谢。

import pycurl
import os

headers = [ "Content-Type: text/xml; charset: UTF-8; " ]
url = "http://myurl/webservice.wsdl"
class FileReader:
    def __init__(self, fp):
        self.fp = fp
    def read_callback(self, size):
        text = self.fp.read(size)
        text = text.replace('\n', '')
        text = text.replace('\r', '')
        text = text.replace('\t', '')
        text = text.strip()
        return text

c = pycurl.Curl()
filename = 'my.xml'
fh = FileReader(open(filename, 'r'))

filesize = os.path.getsize(filename)
c.setopt(c.URL, url)
c.setopt(c.POST, 1)
c.setopt(c.HTTPHEADER, headers)
c.setopt(c.READFUNCTION , fh.read_callback)
c.setopt(c.VERBOSE, 1)
c.setopt(c.HTTP_VERSION, c.CURL_HTTP_VERSION_1_0)
c.perform()
c.close()
# This is the curl command I'm using and it works
# curl -d @my.xml -0 "http://myurl/webservice.wsdl" -H "Content-Type: text/xml; charset=UTF-8"

i'm trying to POST a file to a webservice using CURL (that's what I need to use so I can't take twisted or something else). The problem is that when using pyCurl the webservice doesn't receive the file i'm sending, as in the case commented at the bottom of the file. What am I doing wrong in my pyCurl script? Any ideeas?

Thank you very much.

import pycurl
import os

headers = [ "Content-Type: text/xml; charset: UTF-8; " ]
url = "http://myurl/webservice.wsdl"
class FileReader:
    def __init__(self, fp):
        self.fp = fp
    def read_callback(self, size):
        text = self.fp.read(size)
        text = text.replace('\n', '')
        text = text.replace('\r', '')
        text = text.replace('\t', '')
        text = text.strip()
        return text

c = pycurl.Curl()
filename = 'my.xml'
fh = FileReader(open(filename, 'r'))

filesize = os.path.getsize(filename)
c.setopt(c.URL, url)
c.setopt(c.POST, 1)
c.setopt(c.HTTPHEADER, headers)
c.setopt(c.READFUNCTION , fh.read_callback)
c.setopt(c.VERBOSE, 1)
c.setopt(c.HTTP_VERSION, c.CURL_HTTP_VERSION_1_0)
c.perform()
c.close()
# This is the curl command I'm using and it works
# curl -d @my.xml -0 "http://myurl/webservice.wsdl" -H "Content-Type: text/xml; charset=UTF-8"

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

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

发布评论

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

评论(3

↙温凉少女 2024-10-09 09:57:29

PyCurl 似乎是一个孤立的项目。已经两年没有更新了。我只是将命令行curl 称为子进程。

import subprocess

def curl(*args):
    curl_path = '/usr/bin/curl'
    curl_list = [curl_path]
    for arg in args:
        # loop just in case we want to filter args in future.
        curl_list.append(arg)
    curl_result = subprocess.Popen(
                 curl_list,
                 stderr=subprocess.PIPE,
                 stdout=subprocess.PIPE).communicate()[0]
    return curl_result 

curl('-d', '@my.xml', '-0', "http://myurl/webservice.wsdl", '-H', "Content-Type: text/xml; charset=UTF-8")

PyCurl seems to be an orphaned project. It hasn't been updated in two years. I just call command line curl as a subprocess.

import subprocess

def curl(*args):
    curl_path = '/usr/bin/curl'
    curl_list = [curl_path]
    for arg in args:
        # loop just in case we want to filter args in future.
        curl_list.append(arg)
    curl_result = subprocess.Popen(
                 curl_list,
                 stderr=subprocess.PIPE,
                 stdout=subprocess.PIPE).communicate()[0]
    return curl_result 

curl('-d', '@my.xml', '-0', "http://myurl/webservice.wsdl", '-H', "Content-Type: text/xml; charset=UTF-8")
情愿 2024-10-09 09:57:29

尝试以这种方式上传文件:

c.setopt(c.HTTPPOST, [("filename.xml", (c.FORM_FILE, "/path/to/file/filename.xml"))])

Try to do the file upload in this manner:

c.setopt(c.HTTPPOST, [("filename.xml", (c.FORM_FILE, "/path/to/file/filename.xml"))])

小苏打饼 2024-10-09 09:57:29

解决此类问题可能会很痛苦,因为事先并不总是清楚问题是 1) 您的代码,2) 您正在使用的库,3) Web 服务——还是某种组合。

据观察,PyCURL 并不是一个真正活跃的项目。考虑在 httplib2 之上重写。在许多使用 HTTP 的 Python 库中,它可能是重新创建使用 CURL 所做的事情的最佳候选者。

Troubleshooting problems like this can be a pain because it isn't always clear up front whether the problem is 1) your code, 2) the library you're using, 3) the web service -- or some combination.

It was observed already that PyCURL is not really an active project. Consider rewriting on top of httplib2 instead. Of the many Python libraries that speak HTTP it may be the best candidate for recreating stuff you'd do with CURL.

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