Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python3中为测试建立本地主机时出错_Python 3.x_Multithreading_Testing_Networking_Server - Fatal编程技术网

Python 3.x 在python3中为测试建立本地主机时出错

Python 3.x 在python3中为测试建立本地主机时出错,python-3.x,multithreading,testing,networking,server,Python 3.x,Multithreading,Testing,Networking,Server,我的目标是: 在示例端口3000(或8000)启动本地主机服务器 执行get请求以检查网站是否可用 停止本地主机服务器运行5秒钟 再次启动同一本地主机端口并检查其状态 我所尝试的: 测试班 class MyTestCase(unittest.TestCase): def test_something(self): server = Server() threading.Thread(target=server.create_server, daemon=

我的目标是:

  • 在示例端口3000(或8000)启动本地主机服务器
  • 执行get请求以检查网站是否可用
  • 停止本地主机服务器运行5秒钟
  • 再次启动同一本地主机端口并检查其状态
  • 我所尝试的:

    测试班

    
    class MyTestCase(unittest.TestCase):
        def test_something(self):
            server = Server()
            threading.Thread(target=server.create_server, daemon=True).start()
            server.shutdown()
            try:
                requests.get('http://localhost:8090/')
            except Exception as e:
                raise SystemExit(e)
    
    服务器类:

    class Server():
        def __init__(self):
            self.httpd = None
    
        def shutdown(self):
            self.httpd.shutdown()
    
        def create_server(self):
            PORT = 8090
            Handler = http.server.SimpleHTTPRequestHandler
    
            with socketserver.TCPServer(("", PORT), Handler) as self.httpd:
                print("serving at port", PORT)
                print(self.httpd)
                self.httpd.serve_forever()
                print('never reaches this point')
    
    我的问题: 目前,我的代码无法关闭服务器。我想在某个时候关闭服务器。我尝试了
    server.shutdown()
    &
    server.socket().close()
    。然而,它似乎仍然不起作用

    提前谢谢你