how do i PUT or POST to a web server?

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Mon Oct 16 07:25:34 EDT 2000


In article <39e8e8ff.41ae1 at newton.pacific.net.au>, astuart at NO.mira.SPAMnet 
(Andrew) wrote:

> Hello
> 
> Can anyone point me to an example of how I PUT or POST data to a web 
> server
> using Python?

Dunno about PUT, but for POST you supply a second value to urllib.urlopen 
like:

file = urllib.urlopen(url, urllib.urlencode(dataAsDictonary))

If you want more low-level control, I've found this code to do much the 
same thing:

import httplib, urllib

def postDataReturnResponse(server, file, data):
  message = urllib.urlencode(data)
  h = httplib.HTTP(server)
  h.putrequest('POST', file)
  h.putheader('Accept', 'text/html')
  h.putheader('Content-type', 'application/x-www-form-urlencoded')
  h.putheader('Content-length', str(len(message)))
  h.endheaders()
  h.send(message)
  h.getreply()
  return h.getfile()



More information about the Python-list mailing list