Python 2.7 has reached end of support
and will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Python
2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend that
youmigrate to the latest supported version of Python.
Stay organized with collectionsSave and categorize content based on your preferences.
Traffic from sockets is billed asoutgoing bandwidth.
Async IO (such asTwistedin
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:
importsocket
Libraries that importsocket, such aspoplibornntplib, and that don't violate thelimitations and restrictionslisted
below, should work without modification.
Although the Python 2 runtime supports sockets, there are certain
limitations and behaviors you need to be aware of when using sockets.
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 togetsockoptwill return a mock value and
calls tosetsockoptwill be silently ignored. Errors will continue to be
raised for unsupported options.
The supported options are:
SO_KEEPALIVE
SO_DEBUG
TCP_NODELAY
SO_LINGER
SO_OOBINLINE
SO_SNDBUF
SO_RCVBUF
SO_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 to
usesocketto get around urlfetch limits, you can do so by changing this
default 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 the
submission port 587.
Private, broadcast, multicast, and Google IP ranges are blocked, except
those 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
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 10 minutes of inactivity; any socket operation
keeps 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 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 yourapp.yamlfile 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 this
so thathttplibuses sockets instead, add the following environment variable
to yourapp.yamlfile:
env_variables:GAE_USE_SOCKETS_HTTPLIB:'anyvalue'
You can replaceanyvaluewith any value including an empty string.
App Engine sample using sockets
For a sample using sockets, see thesocket demo
appin the Google Cloud Platform GitHub.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-29 UTC."],[[["\u003cp\u003eThis Sockets API is only functional within the Python 2 runtime, and it's recommended to use an alternative solution before migrating to the Python 3 runtime.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine supports the standard Python socket module API, but only for outbound sockets, and libraries like \u003ccode\u003epoplib\u003c/code\u003e or \u003ccode\u003enntplib\u003c/code\u003e can work if they adhere to specified limitations.\u003c/p\u003e\n"],["\u003cp\u003eSockets have specific restrictions in App Engine, including the inability to create listen sockets, blocked protocols such as FTP, and the blocking of specific IP ranges.\u003c/p\u003e\n"],["\u003cp\u003eSocket descriptors can be passed between App Engine instances, for example, from a frontend to a backend instance.\u003c/p\u003e\n"],["\u003cp\u003eBy default, \u003ccode\u003ehttplib\u003c/code\u003e uses the URL fetch API, but you can configure it to use sockets instead by setting the \u003ccode\u003eGAE_USE_SOCKETS_HTTPLIB\u003c/code\u003e environment variable in your \u003ccode\u003eapp.yaml\u003c/code\u003e file.\u003c/p\u003e\n"]]],[],null,["# Sockets API for legacy bundled services\n\n| **Caution:** **This solution is no longer recommended:** Apps that use this API can only run in the Python 2 runtime and will need to upgrade to a [recommended solution](/appengine/migration-center/standard/services/migrating-services) before migrating to the Python 3 runtime.\n\nTraffic from sockets is billed as [outgoing bandwidth](/appengine/docs/pricing#Billable_Resource_Unit_Costs). Async IO (such as [Twisted](https://docs.twisted.org/) 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:\n\n\u003cbr /\u003e\n\n import socket\n\nLibraries that import `socket`, such as\n`poplib` or `nntplib`, and that don't violate the\n[limitations and restrictions](#limitations-and-restrictions) listed\nbelow, should work without modification.\n\nAlthough the Python 2 runtime supports sockets, there are certain\nlimitations and behaviors you need to be aware of when using sockets.\n\nYou can pickle a socket descriptor and pass it between App Engine\ninstances, such as part of a Task payload. In this scenario, you can open a\nsocket on a frontend instance, and then pass it to a backend instance and use it\nthere.\n\nIn SDK versions prior to 1.8.1, you could\nnot call get/set options against sockets. (Doing so raised \"Not Implemented\"\nexceptions.) However, the Sockets API now allows this.\n\nFor supported options,\ncalls to `getsockopt` will return a mock value and\ncalls to `setsockopt` will be silently ignored. Errors will continue to be\nraised for unsupported options.\n\n\nThe supported options are:\n\n- `SO_KEEPALIVE`\n- `SO_DEBUG`\n- `TCP_NODELAY`\n- `SO_LINGER`\n- `SO_OOBINLINE`\n- `SO_SNDBUF`\n- `SO_RCVBUF`\n- `SO_REUSEADDR`\n\nLimitations and restrictions\n----------------------------\n\nSocket support in App Engine has the following limitations:\n\n- You cannot create a listen socket; you can only create outbound sockets.\n- FTP is not supported.\n- By default, `httplib` is configured to use the [urlfetch api](/appengine/docs/legacy/standard/python/issue-requests); if you need to use `socket` to get around urlfetch limits, you can do so by changing this default so `httplib` uses sockets instead. For more information, see [Making `httplib` use sockets](#making_httplib_use_sockets).\n- You can only use TCP or UDP; arbitrary protocols are not allowed.\n- You cannot bind to specific IP addresses or ports.\n- Port 25 (SMTP) is blocked; you can still use authenticated SMTP on the submission port 587.\n- Private, broadcast, multicast, and Google IP ranges are blocked, except\n those listed below:\n\n - Google Public DNS: `8.8.8.8`, `8.8.4.4`, `2001:4860:4860::8888`, `2001:4860:4860::8844` port 53\n - Gmail SMTPS: `smtp.gmail.com` port 465 and 587\n - Gmail POP3S: `pop.gmail.com` port 995\n - Gmail IMAPS: `imap.gmail.com` port 993\n\n | **Note:** Google Compute Engine IP addresses are not considered to be in Google IP ranges. You can use sockets to connect App Engine apps to Compute Engine instances.\n- Socket descriptors are associated with the App Engine app that\n created them and are non-transferable (cannot be used by other apps).\n\n- Sockets may be reclaimed after 10 minutes of inactivity; any socket operation\n keeps the socket alive for a further 10 minutes.\n\n- Currently, `socket.gethostbyaddr()` is not implemented in Python.\n You can still use the Python SMTP standard library\n (`smtplib`) to open a connection:\n\n # Open a connection to my mail server\n s = smtplib.SMTP('smtp.mailhostingcompany.net', 587)\n\nUsing sockets with the development server\n-----------------------------------------\n\nYou can run and test code using sockets on the development server, without using\nany special command line parameters.\n\nUsing sockets with OpenSSL\n--------------------------\n\nApp Engine supports native Python OpenSSL for the Python 2.7 runtime.\nYou must configure your `app.yaml` file to load the ssl library, as described in\n[OpenSSL Support](/appengine/docs/legacy/standard/python/sockets/ssl_support).\n\nDisabling URL Fetch from handling all outbound requests\n-------------------------------------------------------\n\nIf you import `httplib`, by default it will use the\n[urlfetch api](/appengine/docs/legacy/standard/python/issue-requests). To change this\nso that `httplib` uses sockets instead, add the following environment variable\nto your `app.yaml` file: \n\n env_variables:\n GAE_USE_SOCKETS_HTTPLIB : 'anyvalue'\n\nYou can replace `anyvalue` with any value including an empty string.\n\n\nApp Engine sample using sockets\n-------------------------------\n\nFor a sample using sockets, see the [socket demo\napp](https://github.com/GoogleCloudPlatform/appengine-sockets-python-java-go)\nin the Google Cloud Platform GitHub."]]