【问题标题】:What's the proper way to close a socket & avoid Errno 10054 with python/django使用 python/django 关闭套接字并避免 Errno 10054 的正确方法是什么
【发布时间】:2013-02-21 01:19:48
【问题描述】:

我正在与 django-selenium 合作,在我参与的 django 应用程序上运行 Selenium 测试。这个 django 应用使用 Qt 和本地网络服务器在本地运行。

所以要运行测试,我需要启动应用程序的服务器、selenium 服务器,然后是 webdriver 实例来执行测试。

django-selenium 使用subprocess.Popen('java -jar <path_to_server.jar>') 设置它的服务器,然后如果服务器没有运行,我会为应用程序运行我们的网络服务器;

def run():
    path = os.path.join(os.getcwd(), 'main.py')
    server_running = is_server_running()

    if server_running is False:
        subprocess.Popen(['python', path, '-a'])

现在在测试设置中看起来像这样;

def setUp(self):
    self.server = Process(target= startServer.run)
    self.server.start()

拆解;

def tearDown(self):
    # stop our server
    self.ff.get('http://localhost:{0}/QUIT'.format(settings.LISTEN_PORT))
    # stop the selenium server
    self.ff.get('http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer')
    # close the browser
    self.ff.quit()
    self.server.terminate()

现在在执行此操作时,我会收到一个error: [Errno 10054] An existing connection was forcibly closed by the remote host。我尝试在调用之间添加sleep 以关闭连接,但这没有帮助。

你能看出我可能在哪里犯了错误吗?我认为如果关闭来自远程主机,那么如果我先关闭我们的服务器,然后关闭 selenium 服务器,然后在服务器关闭后终止进程,应该不会有问题。

【问题讨论】:

    标签: python django unit-testing selenium


    【解决方案1】:

    我也遇到了这个问题,我通过在退出前刷新浏览器解决了这个问题。 (是的,很奇怪,我知道)。尝试在self.ff.quit() 之前添加此行:

    self.ff.refresh()
    self.ff.quit()
    

    这为我解决了问题,虽然我不知道为什么。

    【讨论】:

    • 2 年后,这个奇怪的修复仍然有效,谢谢!我正在使用带有硒 2.48.0 的 Django 1.8.4。
    猜你喜欢
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 2012-11-08
    • 2021-05-04
    • 1970-01-01
    • 2016-05-12
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多