这是我的http服务器:
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()
我需要访问我手中的实例t1。
有什么方法可以做到吗?
谢谢!
答案 0 :(得分:15)
稍微好一点的版本,其中t1对于每个实例都不相同。
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
class test:
def show(self):
return "aaaa"
class http_server:
def __init__(self, t1):
def handler(*args):
myHandler(t1, *args)
server = HTTPServer(('', 8080), handler)
server.serve_forever()
class myHandler(BaseHTTPRequestHandler):
def __init__(self, t1, *args):
self.t1 = t1
BaseHTTPRequestHandler.__init__(self, *args)
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()
答案 1 :(得分:9)
有一种方法可以将属性设置为类:
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()
你必须小心,你使用myHandler的每一个地方都是t1的同一个实例
答案 2 :(得分:7)
我知道我很晚才回答,但这可能对未来的观众有所帮助。有一种非常简单的方法可以访问t1
,就像使用server
对象的BaseHTTPRequestHandler
变量所要求的那样:
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
class test:
def show(self):
return "aaaa"
class http_server:
def __init__(self, t1):
server = HTTPServer(('', 8080), myHandler)
server.t1 = t1
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.server.t1.show())
return
class main:
def __init__(self):
self.t1 = test()
self.server = http_server(self.t1)
if __name__ == '__main__':
m = main()
答案 3 :(得分:0)
这个 8 岁问题的另一种答案:) 我也想避免使用在所有 Handler 对象之间共享的类变量以防万一,但我花了一分钟才明白为什么@rsmoorthy 的答案有效。这与他的答案基本相同,但使用 functools.partial
来增加清晰度。
由于 HTTPServer 期望处理程序为 RequestHandlerClass,我们无法完全实例化该类。但是我们可以给我们的子类一个接受额外数据的自定义 __init__
,然后使用 functools.partial
为 HTTPServer 填充额外的信息。这样它只是像它想要的那样实例化我们的处理程序,但我们仍然在里面传递了 t1
。
from http.server import BaseHTTPRequestHandler, HTTPServer
import functools
class test:
def show(self):
return b"aaaa"
class http_server:
def __init__(self, t1):
handler_partial = functools.partial(Handler, t1=t1)
server = HTTPServer(('', 8080), handler_partial)
server.serve_forever()
class Handler(BaseHTTPRequestHandler):
def __init__(self, *args, t1=None, **kwargs):
# Assign before super().__init__ because init is what triggers parsing the request
self.t1 = t1
super().__init__(*args, **kwargs)
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(self.t1.show())
return
class main:
def __init__(self):
self.t1 = test()
self.server = http_server(self.t1)
if __name__ == '__main__':
m = main()