POST-ing issues with pyCurl

I am trying to execute a POST file in a web service using CURL (which I need to use, so I cannot use twisted or anything else). The problem is that when using pyCurl webservice does not receive the file that I am sending, as in the case indicated at the bottom of the file. What am I doing wrong in my pyCurl script? Any ideas?

Many thanks.

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"
+3
source share
3 answers

PyCurl seems like an orphan project. It was not updated after two years. I just call the 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")
+8
source

Try downloading the file this way:

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

+1

Solving problems like this can be painful because it is not always clear if the problem is 1) your code, 2) the library used, 3) the web service or some combination.

It has already been noted that PyCURL is not a truly active project. Instead, consider rewriting on top of httplib2 . Of the many Python libraries that speak HTTP, this may be the best candidate to recreate the stuff you would do with CURL.

0
source

Source: https://habr.com/ru/post/1774292/


All Articles