Python 2.7 has reached end of supportand will bedeprecatedon January 31, 2026. After deprecation, you won't be able to deploy Python 2.7applications, even if your organization previously used an organization policy tore-enable deployments of legacy runtimes. Your existing Python2.7 applications will continue to run and receive traffic after theirdeprecation date. We recommend thatyoumigrate to the latest supported version of Python.

Sockets API for legacy bundled services

Caution:This solution is no longer recommended: Apps that use this API canonly run in the Python 2 runtime and will need to upgrade to arecommended solution before migrating to the Python 3 runtime.
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:

importsocket

Libraries 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_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:

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.