使用 pyCurl 进行 POST 时出现问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PyCurl 似乎是一个孤立的项目。已经两年没有更新了。我只是将命令行curl 称为子进程。
PyCurl seems to be an orphaned project. It hasn't been updated in two years. I just call command line curl as a subprocess.
尝试以这种方式上传文件:
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"))])
解决此类问题可能会很痛苦,因为事先并不总是清楚问题是 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.