Python for reverse output (reverse auto-reply)

Source: Internet
Author: User
Tags cdata

' Shiyanlou ' # here, is left closed right open, is the angle mark is greater than or equal to 1 and less than 3, is the number, so is ' Hi ' ' Hi ' # here, equals S[0:7], because the increment of the value of 1 is the default, you can also change the ' shiyanl ' >>> s[0:7:2] #递增2, look at the results, on the "Sial" # In fact, S is not only ' Shiyanlou ', but also ' Shiyanloushiyanlou ', the middle of the S-angle is 0, and 0 front is a negative number of ' Shiyan ' # less a s, not very good, because the right open, subscript 0 is not output of ' Uolnayih ' >>> s[::-1] #这样就可以了, do not fill, default all, OK, completed the ' UOLNAYIHS '

The simplest way to do this is to use a For loop, request a string of the same size, reverse-order the value and return it to the server, but one of the most convenient ways to use Python is to use the slice of the string to reverse it.

Can run the. py

defWechat_auth ():ifRequest.method = ='GET':        Print 'coming Get'Data=Request.args Token='*****************'Signature= Data.get ('Signature',"') Timestamp= Data.get ('timestamp',"') nonce= Data.get ('nonce',"') Echostr= Data.get ('Echostr',"') s=[Timestamp,nonce,token] S.sort () s="'. Join (s)if(HASHLIB.SHA1 (s). Hexdigest () = =signature):returnmake_response (ECHOSTR)ifRequest.method = ='POST': #接收发来的文本消息, not a GET request, but a POST request, so operate in the process POST : Xml_str=request.stream.read () XML=et.fromstring (xml_str) tousername=xml.find ('Tousername'). Text Fromusername= Xml.find ('Fromusername'). Text Createtime= Xml.find ('Createtime'). Text Msgtype= Xml.find ('Msgtype'). Text

#A normal process, first read the sender's recipient and write time, there is a special important is the type of letter, support text, pictures, video, audio and so on ...

#我们是回复文本信息, if it's not a text message, we'll respond with a word of caution:

ifMsgtype! ='text': Reply=" "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[%s]]></msgtype> <content><! [cdata[%s]]></content> </xml>" "%(Fromusername, Tousername, Createtime,'text',                 'unknow Format, please check out'                )            returnreply

#如果信息不是文本类, return Unknow Format, Please check out .

#如果是文本呢, we continue to read the text content and text information ID, and then reverse, assign value, and finally reply:

content= Xml.find ('Content'). Text MsgId= Xml.find ('MsgId'). TextifType (content).__name__=="Unicode": Content= Content[::-1] Content= Content.encode ('UTF-8')        elifType (content).__name__=="Str":            PrintType (content).__name__content= Content.decode ('Utf-8') Content= Content[::-1] Reply=" "<xml> <tousername><! [cdata[%s]]></tousername> <fromusername><! [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <MsgType> <! [cdata[%s]]></msgtype> <content><! [cdata[%s]]></content> </xml>" "%(Fromusername, Tousername, Createtime, Msgtype, content)returnreplyif __name__=="__main__": App.run (Host='0.0.0.0', port=8080)

Python for reverse output (reverse auto-reply)

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.