【问题标题】:Use python to check if dev_appserver is running on localhost使用 python 检查 dev_appserver 是否在 localhost 上运行
【发布时间】:2011-04-22 16:05:56
【问题描述】:

我有一个脚本用于连接到 localhost:8080 以在 dev_appserver 实例上运行一些命令。我使用remote_api_stub 和httplib.HTTPConnection 的组合。在我对任一 api 进行任何调用之前,我想确保服务器实际上正在运行。

在 python 中确定的“最佳实践”方式是什么:

  1. 如果 any Web 服务器在 localhost:8080 上运行
  2. 如果 dev_appserver 在 localhost:8080 上运行?

【问题讨论】:

    标签: python google-app-engine http


    【解决方案1】:

    应该这样做:

    import httplib
    
    NO_WEB_SERVER = 0
    WEB_SERVER = 1
    GAE_DEV_SERVER_1_0 = 2
    def checkServer(host, port, try_only_ssl = False):
        hh = None
        connectionType = httplib.HTTPSConnection if try_only_ssl \
                                                 else httplib.HTTPConnection
        try:
            hh = connectionType(host, port)
            hh.request('GET', '/_ah/admin')
            resp = hh.getresponse()
            headers = resp.getheaders()
            if headers:
                if (('server', 'Development/1.0') in headers):
                    return GAE_DEV_SERVER_1_0|WEB_SERVER
                return WEB_SERVER
        except httplib.socket.error:
            return NO_WEB_SERVER
        except httplib.BadStatusLine:
            if not try_only_ssl:
                # retry with SSL
                return checkServer(host, port, True)
        finally:
            if hh:
                hh.close()
        return NO_WEB_SERVER
    
    print checkServer('scorpio', 22)   # will print 0 an  ssh server
    print checkServer('skiathos', 80)  # will print 1 for an apache web server
    print checkServer('skiathos', 8080) # will print 3, a GAE Dev Web server
    print checkServer('no-server', 80) # will print 0, no server
    print checkServer('www.google.com', 80) # will print 1
    print checkServer('www.google.com', 443) # will print 1
    

    【讨论】:

    • 我注意到您没有关闭套接字连接 - 如果您没有明确关闭它,连接将保持活动多长时间?是否应该有一个finally 来确保套接字被关闭?
    【解决方案2】:

    我有一个使用 remote_api 执行操作的 ant 构建脚本。为了验证服务器是否正在运行,我只使用 curl 并确保它没有返回错误。

      <target name="-local-server-up">
        <!-- make sure local server is running -->
        <exec executable="curl" failonerror="true">
          <arg value="-s"/>
          <arg value="${local.host}${remote.api}"/>
        </exec>
        <echo>local server running</echo>
      </target>
    

    你可以在 Python 中使用 call 来做同样的事情(假设你的机器上有 curl)。

    【讨论】:

      【解决方案3】:

      我会选择这样的:

      import httplib
      
      GAE_DEVSERVER_HEADER = "Development/1.0"
      
      def is_HTTP_server_running(host, port, just_GAE_devserver = False):
         conn= httplib.HTTPConnection(host, port)
         try:
             conn.request('HEAD','/')
             return not just_GAE_devserver or \
                   conn.getresponse().getheader('server') == GAE_DEVSERVER_HEADER   
         except (httplib.socket.error, httplib.HTTPException):
             return False
         finally:
             conn.close()  
      

      测试:

      assert is_HTTP_server_running('yahoo.com','80') == True
      assert is_HTTP_server_running('yahoo.com','80', just_GAE_devserver = True) == False
      assert is_HTTP_server_running('localhost','8088') == True
      assert is_HTTP_server_running('localhost','8088', just_GAE_devserver = True) == True
      assert is_HTTP_server_running('foo','8088') == False
      assert is_HTTP_server_running('foo','8088', just_GAE_devserver = True) == False
      

      【讨论】:

        猜你喜欢
        • 2020-03-11
        • 2013-03-22
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 2011-06-14
        • 1970-01-01
        • 1970-01-01
        • 2019-03-10
        相关资源
        最近更新 更多