An alternative to PycURL?

Here is some code that loads the file:

  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()

Now I do not like this PycURL experience. Can you offer any alternative? Maybe urllib2 or httplib can do the same? Can you write code showing it?

Thank you very much!

+3
source share
3 answers

Using 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
+1
source

Yes, pycurl has a poor API design, cURL is powerful. It has more futures, then 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 ..

+4

your example is converted to 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()
0
source

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


All Articles