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. Async IO (such as Twisted in Python) is not supported. 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.
Although App Engine supports sockets, 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_KEEPALIVESO_DEBUGTCP_NODELAYSO_LINGERSO_OOBINLINESO_SNDBUFSO_RCVBUFSO_REUSEADDR
Limitations and restrictions
Although App Engine supports sockets, there are certain limitations and behaviors you need to be aware of when using sockets:
- Sockets are available only for paid apps.
- You cannot create a listen socket; you can only create outbound sockets.
- FTP is not supported.
- By default,
httplibis configured to use the urlfetch api; if you need to usesocketto get around urlfetch limits, you can do so by changing this default sohttplibuses sockets instead. For more information, see Makinghttplibuse sockets. - You can only use TCP or UDP; arbitrary protocols are not allowed.
- You cannot 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::8844port 53 - Gmail SMTPS:
smtp.gmail.comport 465 and 587 - Gmail POP3S:
pop.gmail.comport 995 - Gmail IMAPS:
imap.gmail.comport 993
- Google Public DNS:
-
Socket descriptors are associated with the App Engine app that created them and are non-transferable (cannot be used by other apps).
- Sockets may be reclaimed after 2 minutes of inactivity; any socket operation keeps the socket alive for a further 2 minutes.
-
Currently,
socket.gethostbyaddr()is not implemented in Python. You can still use the Python SMTP standard library (smtplib) to open a connection:# Open a connection to my mail server s = smtplib.SMTP('smtp.mailhostingcompany.net', 587)
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.
Making httplib use sockets
If you import httplib, by default it will use the
urlfetch api. To change this so that
httplib uses sockets instead, you simply add the environment variable to your
app.yaml file for your project.
env_variables:
GAE_USE_SOCKETS_HTTPLIB : 'anyvalue'
The value can be any value (including an empty string). If the variable is not
supplied, httplib will continue to use urlfetch.
App Engine sample using sockets
For a sample using sockets, see the socket demo app in the Google Cloud Platform GitHub.