DisallowedHost – Invalid HTTP_HOST Header “example.appspot.com”

When installing a new Django app on App Engine, I forgot to set the hosts header, and found this error:



The problem here is that I didn’t set the allowed hosts header in the settings file. To fix this, go to settings.py file and set ALLOWED_HOSTS to list a wildcard, like so:

ALLOWED_HOSTS = ['*']

You can see this replicated in Google’s own Django example: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/standard/django/mysite/settings.py#L46 (see screenshot below).

A screenshot of allowed_hosts including a wildcard.

Warning: A wildcard is only acceptable because App Engine’s other security policies are protecting the application. If you move the Django site to another host, make sure you change out the wildcard to the appropriate domain/domains.

Amazon Cloud9 IDE Python 3

I spent today morning pulling my hair out trying to get a Python 3 application running within AWS’ Cloud9 IDE. Essentially the problem is that if you pip install some-package, that package only becomes available within the Python 2 runner, not within the Python 3 runner (the current Cloud9 machine image includes Python 2 aliased as python, and Python 3 available through python3).

Fortunately for me, someone else had the same problem, and an Amazon staffer suggests creating a virtual environment: https://forums.aws.amazon.com/thread.jspa?messageID=822214&tstart=0 . I followed the steps mentioned, but it didn’t work – pip still installed dependencies for Python 2. I wonder if this staffer forgot to mention another step, such as remapping the pip command to a Python 3 install.

What finally did work was calling the pip command through the python3 interpreter, like so:

First, ensure pip is installed:

python3 -m ensurepip --default-pip    

Then upgrade pip:

python3 -m pip install --upgrade pip setuptools wheel

Then you can install any requirements of your app, such as BeautifulSoup:

sudo python3 -m pip install --upgrade bs4

And after all that, my Python 3 app was able to access the BeautifulSoup dependency.

Redirecting From Tumblr To WordPress

I’ve been setting up redirects from my old Tumblr blog to WordPress; here is documentation how I handled it.

Netlify offers static web page hosting, and more importantly, supports redirects and rewrites. It can deploy a static web site from a git repository, or by uploading a folder from your local computer. What I did was set up a git repo with a file named “_redirects” (no quotation marks) and connected it with a Netlify deployment; instructions to do this are available here: https://www.netlify.com/docs/continuous-deployment .

The redirects file should look like this:

/                                https://www.learngoogle.com/                            301
/tagged/*                        https://www.learngoogle.com/tag/:splat                            301
/post/49459687975/*              https://www.learngoogle.com/2013/05/02/test/                            301

This works by:

  1. Mapping the root to forward to learngoogle.com
  2. Remaps Tumblr tags, which would look like “/tagged/exampletag” to “learngoogle.com/tag/exampletag” – the :splat is replaced with any text after /tagged/
  3. Remaps a test post to the real location.

Obviously, all of these redirects are 301 Moved Permanently links. You’ll also need to remap your old domain to point to the Netlify deployment target: https://www.netlify.com/docs/custom-domains/ .

Transitioning To WordPress

Hi all,

I’m transitioning this site from Tumblr to WordPress. I dropped a bunch of posts after 2014 which covered a wide array of App Engine topics – I want to redo those with updated information in 2019, as much has changed with the Google Cloud Platform.

If you have any questions, feel free to contact me using the About page (link above).

Thanks,

Extract And Process XML Using Apps Script

Here’s a short code snippet demonstrating how to retrieve and parse a XML file using Apps Script. First, here’s how to download and parse the file:

var url = "URL to XML";
var xml = UrlFetchApp.fetch(url).getContentText();
var xml_document = XmlService.parse(xml);
var xml_root = xml_document.getRootElement();

Once the file is downloaded, you can retrieve child elements by calling getChild/getChildren. GetChild returns the first instance of the named element, and getChildren returns an array listing every element instance:

var xml_items = xml_root.getChild("channel").getChildren("item");

And finally, here’s how to retrieve the text content of an element:

var title = new String(xml_item.getChild("title").getContent(0));

Golang Users API Example

Here’s an example of how to use the Users API in Go. This example checks to see if the current user is logged in; if not, the user is given a link to log in. If the user is already logged in, then it creates and prints a link to log out.

//Creates an App Engine Context - required to access App Engine services.
c := appengine.NewContext(r)
//Acquire the current user
user := appengineuser.Current(c)
if user == nil {
    url, _ := appengineuser.LoginURL(c, "/")
    fmt.Fprintf(w, `<a href="%s">Sign in</a>`, url)
} else {
    url, _ := appengineuser.LogoutURL(c, "/")
    fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, user, url)
}

Searching For User Request Logs In App Engine

You can search for specific users by using the filter labels function of App Engine logging. Here’s a short example: the following filter searches for all users matching the substring inny :

The search finds the following request logs, where the user vinnyapp (my Gmail account) has logged in: