开发者

Use python to check if dev_appserver is running on localhost

I have a script that I use to connect to localhost:8080 to run some commands on a dev_appserver instance. I use a combination of remote_api_stub and httplib.HTTPConnection. Before I make any开发者_StackOverflow社区 calls to either api I want to ensure that the server is actually running.

What would be a "best practice" way in python to determine:

  1. if any web server is running on localhost:8080
  2. if dev_appserver is running on localhost:8080?


This should do it:

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


I have an ant build script that does stuff using remote_api. To verify the server is running I just use curl and make sure no error is returned by it.

  <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>

You could just use call to do the same in Python (assuming you have curl on your machine).


I would go with something like this:

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()  

tested with:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜