2

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()
chepner
  • 497,756
  • 71
  • 530
  • 681
user3745618
  • 239
  • 1
  • 3
  • 11

2 Answers2

2

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

Community
  • 1
  • 1
user3745618
  • 239
  • 1
  • 3
  • 11
0
class myHandler(BaseHTTPRequestHandler):

    def __init__(self, myVariable, *args, **kwargs):
        self.myVariable = myVariable
        super(myHandler, self).__init__()  

#

server = HTTPServer(('', 8080), lambda *args, **kwargs : \
    myHandler(myVariable, *args, **kwargs))
yv84_
  • 101
  • 2
  • 10
  • TypeError: () takes no arguments (3 given) File "C:\Python27\lib\SocketServer.py", line 323, in finish_request self.RequestHandlerClass(request, client_address, self) – user3745618 Aug 19 '14 at 12:28