python - BaseHTTPRequestHandler with custom instance -
this http server:
from basehttpserver import basehttprequesthandler,httpserver class test: def show(self): return "aaaa" class http_server: def __init__(self, t1): self.t1 = t1 server = httpserver(('', 8080), myhandler) server.serve_forever() class myhandler(basehttprequesthandler): def do_get(self): self.send_response(200) self.send_header('content-type','text/html') self.end_headers() self.wfile.write(self.t1.show()) #doesnt work return class main: def __init__(self): self.t1 = test() self.server = http_server(self.t1) if __name__ == '__main__': m = main()
i need acces instance t1 inside of myhander.
is there way how ?
thanks!
there way set property class :
from basehttpserver import basehttprequesthandler,httpserver class test: def show(self): return "aaaa" class http_server: def __init__(self, t1): myhandler.t1 = t1 server = httpserver(('', 8080), myhandler) server.serve_forever() class myhandler(basehttprequesthandler): t1 = none def do_get(self): self.send_response(200) self.send_header('content-type','text/html') self.end_headers() self.wfile.write(self.t1.show()) #doesnt work return class main: def __init__(self): self.t1 = test() self.server = http_server(self.t1) if __name__ == '__main__': m = main()
you have careful that's every use myhandler that's same instance of t1
Comments
Post a Comment