tags:

views:

343

answers:

1

Hey,

Here's a bit of code that is uploading a 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'm not liking this PycURL experience at all. Can you suggest any alternative? Maybe urllib2 or httplib can do the same? Can you write some code showing it?

Huge thanks!

+1  A: 

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
Steven