这里有一些上传文件的代码:
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可以做同样的事情?你能写一些显示它的代码吗?
非常感谢!
发布于 2009-11-06 04:36:16
使用httplib2
import httplib2
http = httplib2.Http()
f = open('Tea.rdf')
body = f.read()
url = 'http://localhost:8080/openrdf-sesame/repositories/rep/statements'
headers = {'Content-type': 'application/rdf+xml;charset=utf-8'}
resp, content = http.request(url, 'PUT', body=body, headers=headers)
# resp will contain headers and status, content the response body发布于 2011-09-08 16:27:21
是的,pycurl有一个糟糕的API设计,cURL是强大的。它有更多的未来,然后是urllib/urllib2。
也许你想尝试使用human_curl。这是python卷曲包装器。您可以从源代码https://github.com/lispython/human_curl或通过pip: pip install human_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>您还可以读取响应头、cookie等
发布于 2011-06-24 04:46:21
您的示例转换为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()https://stackoverflow.com/questions/1683103
复制相似问题