import base64
import httplib, urllib
class FanfouClient:
_username = ""
_password = ""
_authStr = ""
def __init__(self,username,password):
self._username = username
self._password = password
self._authStr = base64.b64encode(username + ":" + password);
#飯否API使用HTTP Basic認證,使用者名稱和密碼用base64編碼
print "[Authentication][Encoded] ", self._authStr
def sendMessage(self,message):
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/xml",
"Authorization": "Basic " + self._authStr}
params = urllib.urlencode({"status": message})
conn = httplib.HTTPConnection("api.fanfou.com")
conn.request("POST","/statuses/update.xml",params,headers)
response = conn.getresponse()
print "Return code:", response.status, " reason:", response.reason
if response.status == 200:
print "Message successfully sent."
else:
print "Error sending message,check your account"
if __name__ == "__main__":
username = 'xxxxxxx'
password = 'xxxxxxx'
msg = ""
while msg == "":
msg = raw_input("Input some message:")
client = FanfouClient(username,password)
client.sendMessage(msg)