[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\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."]]