Home > other >  Shock!!! Global variables don't run on logic! The prospect of the collapse of Python or distort
Shock!!! Global variables don't run on logic! The prospect of the collapse of Python or distort

Time:12-10

Himself with the flask as WeChat public server framework, developed a "guess Numbers" small game, but the global variable unexpectedly in chaotic state, is very strange, also please feel free to comment!
Paste the code below:
 # coding: utf-8 
The from flask import flask, request, abort, render_template
The import hashlib
The import xmltodict, time
Import the random


# global myvalue, guesstime
Myvalue=https://bbs.csdn.net/topics/2222
Guesstime=0

# constant
# WeChat token token
WECHAT_TOKEN="* * * * * *"


App=Flask (__name__)


@ app. The route ("/connect ", the methods=[" GET ", "POST"])
# def index_html () :
# return render_template (index.html, name='stronger')
Def wechat () :
"" "docking WeChat public server "" "
# receive WeChat server sends the parameters of the
Signature=request. The args. The get (" signature ")
Timestamp=request. The args. The get (" timestamp ")
Nonce=request. The args. The get (" nonce ")

If not all (/signature, timestamp, nonce) :
Abort (400)

# calculated according to the process WeChat signature
Li=[WECHAT_TOKEN, timestamp, nonce]
# sort
Li. Sort ()
# string concatenation
Tmp_str="'. Join (li)
# for sha1 encryption, get the correct signature value
Sign=hashlib. Sha1 (tmp_str. Encode (' utf-8 ')). Hexdigest ()
# to calculate the signature value compared with the signature of the request parameters, if the same, the certificate request from WeChat server
If the sign!=signature:
# said request not WeChat hair
Abort (403)
The else:
# is WeChat send request
. If the request method=="GET" :
# says is the first time meet detail server validation letter
Echostr=request. The args. The get (" echostr ")
If not echostr:
Abort (404)
Return echostr
Elif request. The method=="POST" :
# said WeChat server forwarding messages come
Xml_str=request. Data
If not xml_str:
Abort (400)

# to parse the XML string
Xml_dict=xmltodict. Parse (xml_str)
Xml_dict=xml_dict. Get (" XML ")

# to extract the message type
Msg_type=xml_dict. Get (" MsgType ")

If msg_type=="text" :
Result='
Global guesstime
Global myvalue
Inputvalue=https://bbs.csdn.net/topics/xml_dict.get (" Content ")
If inputvalue 'guess'=https://bbs.csdn.net/topics/=:

Myvalue=https://bbs.csdn.net/topics/random.randint (1000100)
Print (myvalue)
Result='please enter an integer between 1000 ~ 10000'
# elif inputvalue. Isdigit () :
The else:
Print (' you input value is % s' % inputvalue)
Print (myvalue)
If the int (inputvalue) & gt; Myvalue:
Result='big! '
Guesstime +=1
Elif int (inputvalue) & lt; Myvalue:
Result='small! '
Guesstime +=1
The else:
Result=u 'congratulations to guess! \ n can continue to guess the next number! '
# saveMysql (MSG) source, guesstime)
Myvalue=https://bbs.csdn.net/topics/random.randint (1000100)
Guesstime=0

# is said to send text messages
# construct the return value, reply to the user via WeChat server message content
Resp_dict={
"XML" : {
"ToUserName" : xml_dict. Get (" FromUserName "),
"FromUserName" : xml_dict. Get (" ToUserName "),
"CreateTime" : int (time. Time ()),
"MsgType", "text",
"Content" : the result
}
}
The else:
Resp_dict={
"XML" : {
"ToUserName" : xml_dict. Get (" FromUserName "),
"FromUserName" : xml_dict. Get (" ToUserName "),
"CreateTime" : int (time. Time ()),
"MsgType", "text",
"Content" : "Dear I Love you so much,"
}
}
# converts dictionary XML string
Resp_xml_str=xmltodict. Unparse (resp_dict)
# returns message data to the WeChat server
Return resp_xml_str
If __name__=="__main__ ':
App. The run (host="127.0.0.1", the port=80, debug=True)


Run the result is, the value of the random variable myvalue but a random value, but the initial value of global variables when too confusing,
  • Related