简体   繁体   English

Python全局变量修改不起作用

[英]Python Global variable modification does not work

I am trying to update a global variable AUTHEN which refers to the socket's inputstream. 我正在尝试更新一个引用套接字输入流的全局变量AUTHEN

In the run() method, I am trying to modify the AUTHEN's value. run()方法中,我试图修改AUTHEN的值。 However, it never changes when I actually run the program. 但是,当我实际运行程序时,它永远不会改变。 I am sure my server is did send other message other than "something" 我确定我的服务器确实发送了除“某事”之外的其他消息

import threading
import sys
import time
import socket
import ssl

AUTHEN ="Something"
class timer(threading.Thread):

    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.ssl_sock = ssl.wrap_socket(self.sock, ssl_version=ssl.PROTOCOL_SSLv23)
        self.ssl_sock.connect(('localhost',9991))
        self.isrun = True
        threading.Thread.__init__(self)


    def send(self,str):
        self.ssl_sock.send(str + "\n")


    def run(self):
        global AUTHEN
        while self.isrun:
            receive = self.ssl_sock.recv(1024)
            AUTHEN = receive
            print("recv ->" +AUTHEN)
        self.sock.close()
        self.ssl_sock.close()


    def close(self):
        self.isrun == False


    def authentication(self,username,password):
        global AUTHEN
        print "Verifing identity"
        self.ssl_sock.send("U&P"+"sprt"+username+"sprt"+password+'\n')
        while (True):
            print AUTHEN
            if(AUTHEN == str("OK\r\n")):
                return AUTHEN   
            else:   
                print "Please Try again"
                break   

def main():
    client = timer()
    client.start()

#LOG IN 
while(True):
    loginMessage = str(raw_input("Please enter username and password as following format: \n username%password \n"))
    username = loginMessage.split("%")[0]
    password = loginMessage.split("%")[1]
    Result = client.authentication(username,password)
    if(Result == str("OK\r\n")):
        print "LOG IN SUCCESSFULLY"
        print "Welcome:\n","Command to be used:\n","-a filename\n" "-c number\n", "-f filename\n","-h hostname:port\n","-n name\n","-u certificate\n","-v filename certificate\n","otherwise input will be treated as normal message"
        break
if __name__=='__main__':
main()

Thank you. 谢谢。

You need to call main() 你需要调用main()

You need to Start() your thread in order to initialize Run() which will set your global variable. 你需要Start()你的线程来初始化Run(),它将设置你的全局变量。 Your thread.start() is in your main() def. 你的thread.start()在你的main()def中。

import threading

class ThreadClass(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global a
        a = 'Test'
    def main(self):
        thread = ThreadClass()
        thread.start()

a= 'funny'
print a

ThreadClass().main()
print a


>>>funny
>>>Test

尝试从代码中删除两个句子:

global AUTHEN;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM