need sample for file-upload via http with auth

Andrew Clover and-google at doxdesk.com
Fri Jan 31 13:41:10 EST 2003


Marian Foerster <marian.foerster at informatik.tu-chemnitz.de> wrote:

> as multipart/form-data

There's a multipart/form-data encoder in form.py, see the functions
writeFormData and writeFormDataStream.

  http://www.doxdesk.com/software/py/form.html

> file-upload via http with auth

something like:

import os.path, socket, urlparse, base64, form

def httpFileUpload(url, username, password, filename, fieldname):
  auth= base64.encodestring(username+':'+password)
  mimetype= 'application/octet-stream' # ignored by most servers
  urlbits= urlparse.urlparse(url)
  if ':' in urlbits[1]:
    (hostname, port)= urlbits[1].split(':',1)
  else:
    (hostname, port)= urlbits[1], 80
  pathname= urlbits[2]

  s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect((hostname, port))
  s.sendall('POST %s HTTP/1.1\r\n' % pathname)
  s.sendall('Host: %s\r\n' % hostname)
  s.sendall('Authorization: Basic %s\r\n' % auth)
  s.sendall(form.writeFormData({
    fieldname: (filename, os.path.basename(filename), mimetype)
  }))

this is not fully tested but should work

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/




More information about the Python-list mailing list