' 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)