Search code examples
pythonbasehttpserver

Global variables in BaseHttpServer class


I am using the method 'do_GET' of class BaseHttpServer.

I want to do is that successive calls to this method have access to the same shared variable

If I send the first command 0 then 1, I can not access the same variable

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from threading import Thread

class myHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        #Some code
        if comand == 0:
             self.task()
        elif comand = 1:            
            #AttributeError: myHandler instance has no attribute 'var'
            temp = self.var 
        return

    def task(self):
       #Ok no exception 
       self.var = 0

#Main
server = HTTPServer(('', 8080), myHandler)
server.serve_forever()

Solution

  • I solve the problem creating a static class, and using in static class global variables.