Posting an image the using python and pycurl

import os, sys

import pycurl

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

loginurl = ""

# Class which holds a file reference and the read callback

class FileReader:

    def __init__(self, fp):

        self.fp = fp

    def read_callback(self, 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)


# Create the item

c.setopt(c.POST, 1)

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

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

c.setopt(c.VERBOSE, 1)



3 thoughts on “Posting an image the using python and pycurl

  1. Does the tail only work for you when you add “-n”?

    When I tested it on my side, it works with or without the added argument. :)

    • I don’t think it worked for me without the -n flag on ubuntu.

      I think on my box it tried to interpret +2 as a flag in some way, but flags start with -
      That way im sending “+2″ as a parameter to -n