Python PycURL的替代品?

Python PycURL的替代品?,python,pycurl,Python,Pycurl,下面是一段上传文件的代码: file_size = os.path.getsize('Tea.rdf') f = file('Tea.rdf') c = pycurl.Curl() c.setopt(pycurl.URL, 'http://localhost:8080/openrdf-sesame/repositories/rep/statements') c.setopt(pycurl.HTTPHEADER, ["Content-Type: application/rdf+

下面是一段上传文件的代码:

  file_size = os.path.getsize('Tea.rdf')
  f = file('Tea.rdf')
  c = pycurl.Curl()
  c.setopt(pycurl.URL, 'http://localhost:8080/openrdf-sesame/repositories/rep/statements')
  c.setopt(pycurl.HTTPHEADER, ["Content-Type: application/rdf+xml;charset=UTF-8"])
  c.setopt(pycurl.PUT, 1)
  c.setopt(pycurl.INFILE, f)
  c.setopt(pycurl.INFILESIZE, file_size)
  c.perform()
  c.close()
现在,我一点也不喜欢这种PycURL体验。你能建议其他的选择吗?也许urllib2或httplib也可以这样做?你能写一些代码来显示它吗

非常感谢

使用:


您的示例已转换为httplib:

import httplib

host = 'localhost:8080'
path = '/openrdf-sesame/repositories/rep/statements'
path = '/index.html'
headers = {'Content-type': 'application/rdf+xml;charset=utf-8'}

f = open('Tea.rdf')
conn = httplib.HTTPConnection(host)
conn.request('PUT', path, f, headers)
res = conn.getresponse()
print res.status, res.reason
print res.read()

是的,pycurl的API设计很糟糕,cURL很强大。它比urllib/urllib2有更多的未来

也许你想试着用人类的卷发。这是python卷曲包装器。您可以从源代码或通过pip:pip-install-human\u-curl安装它

例如:

>>> import human_curl as hurl
>>> r = hurl.put('http://localhost:8080/openrdf-sesame/repositories/rep/statements',
... headers = {'Content-Type', 'application/rdf+xml;charset=UTF-8'},
... files = (('my_file', open('Tea.rdf')),))
>>> r
    <Response: 201>
>>将人形卷曲作为投掷导入
>>>r=投掷。投掷('http://localhost:8080/openrdf-sesame/repositories/rep/statements',
…headers={'Content-Type','application/rdf+xml;charset=UTF-8'},
…文件=(“我的文件”,打开('Tea.rdf'),)
>>>r

您还可以读取响应标题、cookies等

问题是,现在我正试图发布一个针对此存储的查询。。。卷发给我带来了很多麻烦。所以,我只是想知道是否有其他方法可以做到这一点。再次感谢
>>> import human_curl as hurl
>>> r = hurl.put('http://localhost:8080/openrdf-sesame/repositories/rep/statements',
... headers = {'Content-Type', 'application/rdf+xml;charset=UTF-8'},
... files = (('my_file', open('Tea.rdf')),))
>>> r
    <Response: 201>