Pages

Thursday, January 02, 2014

Google App Engine\Python: Using the task queue with a reverse proxy or SSH tunnel

This is more a note for myself. When using forward, a SSH tunnel Ruby gem from the folks at forwardhq.com, Google App Engine\Python devserver will barf trying to resolve the Host header when dispatching tasks on the task queue. In my case, my tunnel hostname was coming through in the Host header, causing the KeyError in _port_registry.get(port) invocation.

dispatcher.py


  def _resolve_target(self, hostname, path):
    if self._port == 80:
      default_address = self.host
    else:
      default_address = '%s:%s' % (self.host, self._port)
    if not hostname or hostname == default_address:
      return self._module_for_request(path), None

    default_address_offset = hostname.find(default_address)
    if default_address_offset > 0:
      prefix = hostname[:default_address_offset - 1]
      # The prefix should be 'module', but might be 'instance.version.module',
      # 'version.module', or 'instance.module'. These alternatives work in
      # production, but devappserver2 doesn't support running multiple versions
      # of the same module. All we can really do is route to the default
      # version of the specified module.
      if '.' in prefix:
        logging.warning('Ignoring instance/version in %s; multiple versions '
                        'are not supported in devappserver.', prefix)
      module_name = prefix.split('.')[-1]
      return self._get_module_with_soft_routing(module_name, None), None

    else:
      if ':' in hostname:
        port = int(hostname.split(':', 1)[1])
      else:
        port = 80
      try:
        _module, inst = self._port_registry.get(port)
      except KeyError:
        raise request_info.ModuleDoesNotExistError(hostname)
        _module, inst = None, None
    if not _module:
      _module = self._module_for_request(path)
    return _module, inst

The line in red is the line that is barfing. Comment out the raise error line and set _module and inst to None, allowing execution to continue and the next line will test if _module hasn't been set and will go ahead and resolve it. Found the temporary fix from https://github.com/dylanvee/homebrew-gae_sdk. Hopefully this is something the GAE people can fix in upcoming versions of GAE\Python SDK.