need sample for file-upload via http with auth

Andrew Clover and-google at
Fri Jan 31 19:41:10 CET 2003

Marian Foerster <marian.foerster at> wrote:

> as multipart/form-data

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

> 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)
    (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)
    fieldname: (filename, os.path.basename(filename), mimetype)

this is not fully tested but should work

Andrew Clover
mailto:and at

More information about the Python-list mailing list