1

I am trying to update a global variable AUTHEN which refers to the socket's inputstream.

In the run() method, I am trying to modify the AUTHEN's value. 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.

bslqy
  • 233
  • 2
  • 12
  • 4
    Why are you using semi colons in python ?? – anand May 23 '16 at 15:53
  • @anand java habit... – bslqy May 23 '16 at 15:54
  • When in python, do as it is done in python; also specify which python version you are using. – anand May 23 '16 at 15:56
  • And what is the output of that? – Serge Ballesta May 23 '16 at 16:01
  • 1
    Why not define AUTHEN as self.authen in your object and refer to it that way? Also, where are you calling `main()`? – Random Davis May 23 '16 at 16:04
  • you might be overriding `AUTHEN` with your `global` declaration – Evhz May 23 '16 at 16:21
  • Sorry, but if the peer sends `OK\r\n`, this code seems to work correctly. I just tested it with an *interactive* python shell (server socket bound to 9991 in a second idle window) with ssl removed and I correctly got a connection as soon as the peer sent `OK\r\n`. Control your server... – Serge Ballesta May 23 '16 at 16:28
  • @SergeBallesta Hello. My server is a java which sends outputstream.println("OK"). And the `AUTHEN = receive print("recv ->" +AUTHEN)` shows me "Something" ... – bslqy May 24 '16 at 01:19
  • @RandomDavis I want to use the inputstream as a global variable because I might do something with it according to different input message. Basically the client has to log in first. However, the inputstream is already running when I call main so I can't refer to it again in `authentication()` method. Otherwise the input thread will be blocked. So I am trying to extract the message from the inputstream – bslqy May 24 '16 at 01:31
  • @SergeBallesta `def run(self): while self.isrun: receive = self.ssl_sock.recv(1024); print receive +"!!!!!!!" AUTHEN = receive; print("recv ->" +AUTHEN); self.sock.close(); self.ssl_sock.close();` I noticed that `print receive` is not being executed, maybe there is nothing coming from the server.. – bslqy May 24 '16 at 01:43
  • @SergeBallesta And I mannualy send some message to the client, it doesn't receive.. – bslqy May 24 '16 at 01:49
  • I confirm. In my tests, the first message is always `Something` because nothing has still been received from server. The workflow is: 1/ set up a listening server 2/ launch `main()` client side: get `Something` 3/ send `Ok\r\n` from server 4/ next authentication is successful – Serge Ballesta May 24 '16 at 05:58

2 Answers2

0

You need to call main()

You need to Start() your thread in order to initialize Run() which will set your global variable. Your thread.start() is in your 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
EoinS
  • 5,405
  • 1
  • 19
  • 32
  • borrowed sample [from here](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – EoinS May 23 '16 at 16:25
0

Try removing from your code the two sentences:

global AUTHEN;
Evhz
  • 8,852
  • 9
  • 51
  • 69