In Python, how can I test if I'm in Google App Engine SDK?

前端 未结 6 1943
忘了有多久
忘了有多久 2020-11-27 05:19

Whilst developing I want to handle some things slight differently than I will when I eventually upload to the Google servers.

Is there a quick test that I can do to

6条回答
  •  Happy的楠姐
    2020-11-27 05:59

    A more general solution
    A more general solution, which does not imply to be on a Google server, detects if the code is running on your local machine. I am using the code below regardless the hosting server:

    import socket
    
    if socket.gethostname() == "your local computer name":
        DEBUG = True
        ALLOWED_HOSTS = ["127.0.0.1", "localhost", ]
        ...
    else:
        DEBUG = False
        ALLOWED_HOSTS = [".your_site.com",]
        ...
    

    If you use macOS you could write a more generic code:

    if socket.gethostname().endswith(".local"): # True in your local computer
        ...
    

    Django developers must put this sample code in the file settings.py of the project.

    EDIT:
    According to Jeff O'Neill in macOS High Sierra socket.gethostname() returns a string ending in ".lan".

提交回复
热议问题