Sockets API for legacy bundled services Stay organized with collections Save and categorize content based on your preferences.
Traffic from sockets is billed asoutgoing bandwidth.Async IO (such asTwisted inPython) is not supported.App Engine supports the standard Python socket module API for outboundsockets only. You simply import the standard socket library using the followingstatement:
importsocketLibraries that importsocket, such aspoplib ornntplib, and that don't violate thelimitations and restrictions listedbelow, should work without modification.
Although the Python 2 runtime supports sockets, there are certainlimitations and behaviors you need to be aware of when using sockets.
You can pickle a socket descriptor and pass it between App Engineinstances, such as part of a Task payload. In this scenario, you can open asocket on a frontend instance, and then pass it to a backend instance and use itthere.
In SDK versions prior to 1.8.1, you couldnot call get/set options against sockets. (Doing so raised "Not Implemented"exceptions.) However, the Sockets API now allows this.
For supported options,calls togetsockopt will return a mock value andcalls tosetsockopt will be silently ignored. Errors will continue to beraised for unsupported options.The supported options are:
SO_KEEPALIVESO_DEBUGTCP_NODELAYSO_LINGERSO_OOBINLINESO_SNDBUFSO_RCVBUFSO_REUSEADDR
Limitations and restrictions
Socket support in App Engine has the following limitations:
- You cannot create a listen socket; you can only create outbound sockets.
- FTP is not supported.
- By default,
httplibis configured to use theurlfetch api; if you need tousesocketto get around urlfetch limits, you can do so by changing thisdefault sohttplibuses sockets instead. For more information, seeMakinghttplibuse 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 thesubmission port 587.
Private, broadcast, multicast, and Google IP ranges are blocked, exceptthose listed below:
- 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 thatcreated them and are non-transferable (cannot be used by other apps).
Sockets may be reclaimed after 10 minutes of inactivity; any socket operationkeeps the socket alive for a further 10 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 servers=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 usingany special command line parameters.
Using sockets with OpenSSL
App Engine supports native Python OpenSSL for the Python 2.7 runtime.You must configure yourapp.yaml file to load the ssl library, as described inOpenSSL Support.
Disabling URL Fetch from handling all outbound requests
If you importhttplib, by default it will use theurlfetch api. To change thisso thathttplib uses sockets instead, add the following environment variableto yourapp.yaml file:
env_variables:GAE_USE_SOCKETS_HTTPLIB:'anyvalue'You can replaceanyvalue with any value including an empty string.
App Engine sample using sockets
For a sample using sockets, see thesocket demoappin the Google Cloud Platform GitHub.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.