Python urllib2发送即时消息到twitter的实现方法,urllib2twitter,通过学习Python教程


通过学习Python教程可以做出聊天工具,并能做到与常用聊天软件互动的效果。本为就为大家提供了关于Python urllib2发送即时消息到twitter的实现方法的源码分享。其实这就是一个互动聊天工具的实现。

Python发送即时消息的Python源码,需要用到re正则和urllib2与urllib网络资源讯问模块的使用。

Python twitter 即时消息发送工具

Python twitter 即时消息发送工具

Python urllib2发送即时消息到twitter的实现方法,Python源码如下:

import re,urllib2,urllibuser = {'session[username_or_email]':'username_or_email','session[password]':'******'}data = {    'status':"""Send by Python!""",    'tab':'home',    'source':'web',    }def u(s, encoding):    if isinstance(s, unicode):        return s    else:        try:            return unicode(s, encoding)        except:            return sdef send(user=user,data=data):    c = urllib2.HTTPCookieProcessor()    builder = urllib2.build_opener(c)    url = 'https://twitter.com/sessions'    request = urllib2.Request(        url=url,        data = urllib.urlencode(user)        )    d = builder.open(request)    r = re.compile('<input name="authenticity_token" type="hidden" value="(.*?)" />')    x = d.read()    if len(re.compile(r"name=\"session\[username_or_email\]\"").findall(x))>0:        print "Login Error!"        return False    auth = {'authenticity_token':r.findall(x)[0]}    send = '%s&%s'%(        urllib.urlencode(auth),        urllib.urlencode(data)        )    request = urllib2.Request(        url='http://twitter.com/status/update',        data = send ,        )    builder.open(request)    return Trueif __name__=="__main__":    import sys    if len(sys.argv)>1 and sys.argv[1]!="":        data["status"] = u(" ".join(sys.argv[1:]),"gb2312").encode("utf-8")    if send():        print 'ok'

Python即时信息发送相关文章推荐:Python Google talk聊天机器人源码

编橙之家文章,

评论关闭