How do I make Google App Engine python SDK Remote

2019-05-14 16:14发布

Using the remote api (remote_api_shell.py) works fine on the production server. However, it only works on the development server when the development server is serving on localhost. It does not work when the server is running on specific IP (for example, dev_appserver.py --host=192.168.0.1).

This is using the Python SDK. I'm kinda sure this worked on version 1.7.5. It does not work on 1.7.6 or 1.8.0.

Here's a specific case:

Run the server and let it bind to the default address (localhost:8080):

/path/to/dev_appserver.py myapp/app.yaml
INFO     2013-05-25 19:11:15,071 sdk_update_checker.py:244] Checking for updates to the SDK.
INFO     2013-05-25 19:11:15,323 api_server.py:152] Starting API server at: http://localhost:39983
INFO     2013-05-25 19:11:15,403 dispatcher.py:98] Starting server "default" running at: http://localhost:8080
INFO     2013-05-25 19:11:15,405 admin_server.py:117] Starting admin server at: http://localhost:8000

Start the remote API shell, and it works fine:

$ ./remote_api_shell.py -s localhost:8080
Email: x@x
Password: 
App Engine remote_api shell
Python 2.7.2+ (default, Jul 20 2012, 22:15:08) 
[GCC 4.6.1]
The db, ndb, users, urlfetch, and memcache modules are imported.
dev~furloughfun> 

However, if you start the server with a specified host:

/path/to/dev_appserver.py --host=192.168.0.1 myapp/app.yaml
INFO     2013-05-25 19:11:53,304 sdk_update_checker.py:244] Checking for updates to the SDK.
INFO     2013-05-25 19:11:53,554 api_server.py:152] Starting API server at: http://localhost:44650
INFO     2013-05-25 19:11:53,633 dispatcher.py:98] Starting server "default" running at: http://192.168.0.1:8080
INFO     2013-05-25 19:11:53,634 admin_server.py:117] Starting admin server at: http://localhost:8000

Notice it says Starting API server at: http://localhost:44650 even though the content is served at http://192.168.0.1:8080. Is this indicative that you can only run the remote api on localhost? Perhaps for security reasons?

Also, when you try the remote_api_shell.py now, you can only log in with a valid account (no bogus accounts allowed) and it immediately errors and terminates.

The console errors end with:

urllib2.HTTPError: HTTP Error 200: OK

and the local development server outputs:

INFO 2013-05-25 19:24:06,674 server.py:528] "GET /_ah/remote_api?rtok=90927106532 HTTP/1.1" 401 57

Anyone know what's going on here?
Is it impossible to access the remote API other than on localhost?
Is it impossible to access the remote API (even on localhost) if your content is served on a specific IP?

3条回答
The star\"
2楼-- · 2019-05-14 16:43

It seems that api server doesn't have option to set host. dev_appserver.py has options to set host & port for content and admin server, and for api server only port (api_port option). Example:

dev_appserver.py --host=192.168.5.92 
                 --admin_host 192.168.5.92 --admin_port 9000 
                 --api_port 7000 .

Running this reports:

 api_server.py:153] Starting API server at: http://localhost:7000

 dispatcher.py:164] Starting server "default" running at: http://192.168.5.92:8080

 admin_server.py:117] Starting admin server at: http://192.168.5.92:9000

Looking into the source of GAE dev_appserver, caller of the method of api_server which starts the server is module devappserver2.py and the line is:

apis = api_server.APIServer('localhost', options.api_port,
                             configuration.app_id)

You can see hardcoded host name localhost.

If you find no good workaround, I would suggest to patch devappserver2.py by introducing a new option and report issue with patch attached?

查看更多
三岁会撩人
3楼-- · 2019-05-14 16:46

Now, at least from version 1.9.27 of the SDK, the option --api_host helps with that.

查看更多
闹够了就滚
4楼-- · 2019-05-14 16:50

Just an update to this as the GAE file structure has changed. Mine was located at %ProgramFiles%\Google\google_appengine\google\appengine\tools\devappserver2\api_server.py Inside this file you could see the definition for the API server

class APIServer(wsgi_server.WsgiServer):
  """Serves API calls over HTTP."""

  def __init__(self, host, port, app_id):
    self._app_id = app_id
    self._host = host
    super(APIServer, self).__init__((host, port), self)

I tried passing the port argument thru the GUI of the GAE App Launcher, it doesn't seem to work at all, so I just hardcoded this port, otherwise the port is being randomized. and that could also lead to connection errors to the remote api server.

查看更多
登录 后发表回答