Created
August 31, 2011 09:29
-
-
Save ifduyue/1183166 to your computer and use it in GitHub Desktop.
httplib example: publish a fanfou status
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def setcookie2cookie(setcookie): | |
cookies = setcookie.split("\n") | |
result = [] | |
for ck in cookies: | |
frags = ck.split(";") | |
i = frags[0].index("=") | |
name = frags[0][:i] | |
value = frags[0][i+1:] | |
#name = name.replace("+", " ") | |
if name.strip(): | |
result.append([name, value]) | |
return result | |
def cookielist2cookiestring(cookie): | |
cookies = [] | |
for i in cookie: | |
cookies.extend(setcookie2cookie(i)) | |
cookiestring = "; ".join(["%s=%s" % (name, value) for name, value in cookies]) | |
return cookiestring | |
def setcookie2cookiestring(setcookie): | |
cookies = setcookie2cookie(setcookie) | |
return '; '.join(['%s=%s' % (name, value) for name, value in cookies]) | |
def test_fanfou_httplib(username, password, status): | |
import httplib, re, urllib | |
h = httplib.HTTPConnection('m.fanfou.com') | |
h.request('GET', 'http://m.fanfou.com/', None, {"Referer": "http://m.fanfou.com/"}) | |
resp = h.getresponse() | |
data = resp.read() | |
token = re.search('''name="token".*?value="(.*?)"''', data).group(1) | |
post = urllib.urlencode({ | |
'loginname': username, | |
'loginpass': password, | |
'action': 'login', | |
'token' : token, | |
'auto_login': 'on', | |
}) | |
h.request('POST', 'http://m.fanfou.com/home', post, {"Referer": "http://m.fanfou.com/", "Content-Type": "application/x-www-form-urlencoded"}) | |
resp = h.getresponse() | |
cookiestring = cookielist2cookiestring(resp.msg.getheaders('set-cookie')) | |
post = urllib.urlencode({ | |
'content': status, | |
'token' : token, | |
'action': 'msg.post', | |
}) | |
h.request('POST', 'http://m.fanfou.com/home', post, {"Referer": "http://m.fanfou.com/", "Content-Type": "application/x-www-form-urlencoded", "Cookie": cookiestring}) | |
resp = h.getresponse() | |
h.close() | |
if __name__ == '__main__': | |
username, password, status = sys.argv[1:4] | |
test_fanfou_httplib(username, password, status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment