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..."
If you are trying to make a https connection using httplib2.Http to a server which uses self-signed certificate, you might face “httplib2.SSLHandshakeError: [Errno 1] _ssl.c:480: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”.
There are 2 solutions:
1. httplib2.Http(disable_ssl_certificate_validation=True).request(‘https://www.godaddy.com/’)
2. httplib2 uses its own certificate store. Usually, the location would be python/httplib2/cacerts.txt.
Edit this file to add the certificate of your server and you should be good to go.
[ http://viraj-workstuff.blogspot.com/2011/07/python-httplib2-certificate-verify.html ]
Also, be aware of this:
http://daniel.haxx.se/blog/2012/01/27/sloppily-using-ssl_op_all/
Why do you talk about https in the title when the URL you use in the code uses http, not https? Maybe you should fix that
Thanks for pointing that out
have corrected it.
Take a look at requests (http://python-requests.org), as it makes these things very easy.
Didn’t know about python-requests , just checked it out ! it looks great. will definitely give it a try.
Thanks
Your post has title: Pythonic HTTPS access
But i don’t get from your it how it is concerned with https…