Pythonic HTTPS access

NOTE: Twitter Has Disabled Basic Auth as of August 31 2010 so this example will not work

I could have also named this post “Python HTTP POST with Basic Authentication?” But i wanted to keep it simple , and really this post is just meant for documentation purposes.

import urllib
import httplib2
import sys

if len(sys.argv) != 2:
    print "Please enter message"
    raise SystemExit

msg = sys.argv[1]

username = "user"
password = "pass"

http = httplib2.Http()
http.add_credentials(username, password)
response = http.request(
"https://twitter.com/statuses/update.xml",
"POST",
urllib.urlencode({"status": msg})
)

if response:
    print "Update OK!"
else:
    print "Error updating..."
Advertisements