Posting an image the mysites.com using python and pycurl

import os, sys

import pycurl

USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0)'

loginurl = "www.mysites.com/login"

# Class which holds a file reference and the read callback

class FileReader:

    def __init__(self, fp):

        self.fp = fp

    def read_callback(self, size):

        return self.fp.read(size)

# Check commandline arguments

if len(sys.argv) < 3:

    print "Usage: %s
"
% sys.argv[0]

    raise SystemExit

servicetype = sys.argv[1]

filename = sys.argv[2]

itemname = sys.argv[3]

username = sys.argv[4]

password = sys.argv[5]

if not os.path.exists(filename):

    print "Error: the file '%s' does not exist" % filename

    raise SystemExit

# Initialize pycurl

c = pycurl.Curl()

# Request login page

c.setopt(pycurl.USERAGENT, USER_AGENT)

c.setopt(pycurl.FOLLOWLOCATION, 1)

#c.setopt(pycurl.WRITEFUNCTION, dev_null.write)

c.setopt(pycurl.COOKIEFILE, '')

c.setopt(pycurl.URL, loginurl + '?username=' + username + '&password=' + password)

c.perform()

# Create the item

c.setopt(c.POST, 1)

c.setopt(c.URL, "www.mysites.com/" + servicetype + "@core/create_item")

c.setopt(c.HTTPPOST, [("file", (c.FORM_FILE, filename)),("name", itemname),("username_override", "ajaxmmo")])

c.setopt(c.VERBOSE, 1)

c.perform()

c.close()