Build a WeChat public platform using python

Source: Internet
Author: User
Tags sha1 encryption
This article mainly introduces the materials and skills related to setting up a public platform in python. If you are interested, refer to the public platform developed by python Based on Sina sae to achieve the following functions:

Input a paragraph --- reply to a joke
Input open source + Article --- send messages to open source China
Enter express delivery + Order Number --- query express delivery information
Enter weather --- query weather conditions in Nanjing in the last five days
Enter Weibo hotspot --- reply to Weibo's current hot topic
Input movie + name --- reply to link searched in Baidu cloud Disk

Specific implementation code:

#-*-Coding: UTF-8-*-import hashlibimport webimport using timeimport osimport urllib2, jsonimport using reimport using hashlibimport cookielibfrom urllib import urlencodefrom lxml import etree class WeixinInterface: def _ init _ (self): self. app_root = OS. path. dirname (_ file _) self. templates_root = OS. path. join (self. app_root, 'templates') self. render = web. template. render (self. templates_root) def GET (self): # obtain the input parameter data = web. input () signature = data. signature timestamp = data. timestamp nonce = data. nonce echostr = data. echostr # Your token = "weixin9047" # rewrite the token you entered on the public platform # lexicographically sorted list = [token, timestamp, nonce] list. sort () sha1 = hashlib. sha1 () map (sha1.update, list) hashcode = sha1.hexdigest () # sha1 encryption algorithm # if it is a request from, it returns echostr if hashcode = signature: return echostr def POST (self): str_xml = web. data () # Get the post data xml = etree. fromstring (str_xml) # Parse XML content = xml. find ("Content "). text # obtain the content entered by the user msgType = xml. find ("MsgType "). text fromUser = xml. find ("FromUserName "). text toUser = xml. find ("ToUserName "). text if (content = u "Weather"): url =" http://m.ip138.com/21/nanjing/tianqi/ "Headers = {'connection': 'Keep-alive', 'access': 'text/html, application/xhtml + xml, */* ', 'access'-Language ': 'en-US, en; q = 0.8, zh-Hans-CN; q = 0.5, zh-Hans; q = 100', 'user-agent ': 'mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv: 11.0) like Gecko '} req = urllib2.Request (url, headers = headers) opener = urllib2.urlopen (req) html = opener. read () rex = R '(? <= Img src = "/image/s00000-92.16.gif" alt = }(? = "/>) 'Rexx = R '(? <= P class = "temperature">). {5, 15 }(? =

) 'N' = re. findall (rex, html) m = re. findall (rexx, html) str_wether = "" for (I, j) in zip (m, n ): str_wether = str_wether + j + "" + I + "\ n" return self. render. reply_text (fromUser, toUser, int (time. time (), "weather in the last five days: \ n" + str_wether) elif (content [0: 2] = u ""): keyword = urllib. quote (content [2:]. encode ("UTF-8") url =" http://www.wangpansou.cn/s.php?q= "+ Keyword headers = {'connection': 'Keep-alive', 'accept': 'text/html, application/xhtml + xml ,*/*', 'Accept-color': 'en-US, en; q = 0.8, zh-Hans-CN; q = 0.5, zh-Hans; q = 100 ', 'User-agent': 'mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv: 11.0) like Gecko '} req = urllib2.Request (url, headers = headers) opener = urllib2.urlopen (req) html = opener. read () rex = r'https?: // Pan.baidu.com .*\? Uk = [0-9] {10}. * [\ d +?] "'M = re. findall (rex, html) string = u "" for I in m: string = string + I + "\ n" return self. render. reply_text (fromUser, toUser, int (time. time (), u "The following is the movie link: \ n" + string) elif (u "" in content): url_8 =" http://www.qiushibaike.com/ "Url_24 =" http://www.qiushibaike.com/ Hot/"headers = {'connection': 'Keep-alive', 'accept': 'text/html, application/xhtml + xml ,*/*', 'Accept-color': 'en-US, en; q = 0.8, zh-Hans-CN; q = 0.5, zh-Hans; q = 100 ', 'User-agent': 'mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv: 11.0) like Gecko '} req_8 = urllib2.Request (url_8, headers = headers) req_24 = urllib2.Request (url_24, headers = headers) opener_8 = urllib2.urlopen (req_8) opener_24 = url Lib2.urlopen (req_24) html_8 = opener_8.read () html_24 = opener_24.read () rex = R '(? <= P class = "content"> ).*? (? =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.