首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Python中测试运行http.server?

如何在Python中测试运行http.server?
EN

Stack Overflow用户
提问于 2020-02-20 00:11:09
回答 1查看 38关注 0票数 0

我需要测试我的服务器的可用性。我已经写了测试:

代码语言:javascript
运行
复制
class TestApp(unittest.TestCase):
    def setUp(self):
        self.child_pid = os.fork()
        if self.child_pid == 0:
            HTTPServer(('localhost', 8000), Handler).serve_forever()

    def test_app(self):
        try:
            urllib.request.urlopen('http://localhost:8000')
        except (URLError, HTTPError) as e:
            self.fail()

但是在测试通过之后,init采用了几个python进程。测试结束后如何杀除子进程?

EN

回答 1

Stack Overflow用户

发布于 2020-02-20 04:25:24

找到解决方案了。需要使用threading模块:

代码语言:javascript
运行
复制
class TestApp(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.server = ...
        thread = threading.Thread(target=cls.server.serve_forever)
        thread.start()

    @classmethod
    def tearDownClass(cls):
        cls.server.shutdown()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60304636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档