In my previous post, I have published the source codes to build a CLI php app to update your twitter status. Here comes the Python version…. It’s been a pain… took me 15 long minutes to cook it… I was having trouble with the basic authorization twitter requires. I am using Python 3 and had to make the basic auth credentials encoded into binary, base64 encode the binary and then decode the returned binary back into a string before I could use it to authorize
.
Finally, it’s done and I am very much delighted with it… It asks for your username, password and finally the status when run. Feed it the information and it’ll do the job for you…
import sys;
from urllib.request import urlopen,Request;
import base64;username = input(“Username: “);
password = input(“Password: “);
url = “http://www.twitter.com/statuses/update.xml”;
status = input(“Enter Status: “);
data =”status=”+status;
req = Request(url);
req.data = data;
req.headers['Authorization'] = ‘Basic %s’ % (base64.b64encode((username + “:” + password).encode())).decode();
# req.headers['User-Agent'] = ‘Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20′;
urlopen(req);
Thanks to Python… Ireally love it