Google App Engine
Feedback on this document

Socket API Overview

Preview!

This is a Preview release of the Socket API. As a result, the API is subject to change and the service itself is currently not covered by any SLA or deprecation policy. These characteristics will be evaluated as the API and service moves towards General Availability, but developers should take this into consideration when using the Preview release of Socket API.
 

App Engine supports the standard Python socket module API for outbound sockets only. You simply import the standard socket library using the following statement:

import socket

Libraries that import socket, such as poplib or nntplib, and that don't violate the limitations and restrictions listed below, should work without modification.

App Engine supports sockets without requiring any special App Engine libraries or add any special App Engine code. However, there are certain limitations and behaviors you need to be aware of when using sockets.

Notice that you can pickle a socket descriptor and pass it between App Engine instances, such as part of a Task payload. In this scenario, you can open a socket on a frontend instance, and then pass it to a backend instance and use it there.

In SDK versions prior to 1.8.1, you could not call get/set options against sockets. (Doing so raised "Not Implemented" exceptions.) However, the Sockets API now allows this. For supported options, calls to getsockopt will return a mock value and calls to setsockopt will be silently ignored. Errors will continue to be raised for unsupported options. The currently supported options are:

  • SO_KEEPALIVE
  • SO_DEBUG
  • TCP_NODELAY
  • SO_LINGER
  • SO_OOBINLINE
  • SO_SNDBUF
  • SO_RCVBUF
  • SO_REUSEADDR

Note: Sockets are only available for paid apps, and traffic from sockets is billed as outgoing bandwidth. Sockets are also limited by daily and per minute (burst) quotas.

Limitations and Restrictions

The following limitations and restrictions apply to the use of sockets with App Engine:

  • Sockets are available only for paid apps.
  • You can’t create a listen socket; you can only create outbound sockets.
  • httplib is still configured to use the urlfetch api; if you prefer to use socket to get around request size limits then you must upload and use your own version of httplib.
  • You can only use TCP or UDP; arbitrary protocols are not allowed.
  • You can’t bind to specific IP addresses or ports.
  • Port 25 (SMTP) is blocked; you can still use authenticated SMTP on the submission port 587.
  • Private, broadcast, multicast, and Google IP ranges (except those whitelisted below), are blocked:
    • Google Public DNS: 8.8.8.8, 8.8.4.4, 2001:4860:4860::8888, 2001:4860:4860::8844 port 53
    • Gmail SMTPS: smtp.gmail.com port 465 and 587
    • Gmail POP3S: pop.gmail.com port 995
    • Gmail IMAPS: imap.gmail.com port 993
  • Currently, socket.gethostbyaddr() is not implemented in Python. If you are trying to send mail using the Python smtp standard library (smtplib), calls to smtplib.SMTP will raise NotImplementedError() if your code doesn't supply a local_hostname. To work around this, simply supply local_hostname as follows:
    # Open a connection to my mail server
    s = smtplib.SMTP('smtp.mailhostingcompany.net', 587, "mymailhostname")
  • Sockets may be reclaimed after 2 minutes of inactivity; any socket operation (e.g. getpeername) keeps the socket alive for a further 2 minutes, or select can be used for multiple sockets.
  • Socket descriptors are associated with the app-id that created them and are non-transferable (cannot be used by other app-ids).

Using Sockets with the Development Server

You can run and test code using sockets on the development server, without using any special command line parameters.

Using Sockets with OpenSSL

App Engine supports native Python OpenSSL for the Python 2.7 runtime. You must configure your app.yaml file to load the ssl library, as described in OpenSSL Support.

App Engine Sample using Sockets

For a sample using sockets, see the Python socket demo. This sample app uses the Socket API via nntplib to access an NNTP server, and display information about newsgroup articles. The example includes a demo of passing an open socket descriptor to a Task Queue task as part of its payload: the descriptor is later unpickled and accessed when the task is run.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.