WSGI समझें: Python ऐप और सर्वर के बीच request-response अनुबंध
WSGI (Web Server Gateway Interface) वह मानक call contract है जिसके माध्यम से Python web apps और WSGI servers HTTP requests का आदान-प्रदान करते हैं। ऐप एक synchronous callable है जो environ और startresponse लेता है, फिर response bytes का iterable लौटाता है। इस सीमा को समझने से framework के application object को server से जोड़ना और WSGI को ASGI से अलग पहचानना आसान होता है। यह लेख PEP 3333 के WSGI 1.0.1 को समझाता है और मौजूदा ASGI specification से तुलना करता है; यह किसी विशेष framework या server की deployment guide नहीं है।
WSGI (Web Server Gateway Interface) वह मानक call contract है जिसके माध्यम से Python web apps और WSGI servers HTTP requests का आदान-प्रदान करते हैं। ऐप एक synchronous callable है जो environ और start_response लेता है, फिर response bytes का iterable लौटाता है। इस सीमा को समझने से framework के application object को server से जोड़ना और WSGI को ASGI से अलग पहचानना आसान होता है। यह लेख PEP 3333 के WSGI 1.0.1 को समझाता है और मौजूदा ASGI specification से तुलना करता है; यह किसी विशेष framework या server की deployment guide नहीं है।
एक request से response iterable तक
हर request पर WSGI server environment dictionary और response शुरू करने वाला callable तैयार कर ऐप को बुलाता है। Environment में REQUEST_METHOD, PATH_INFO, QUERY_STRING, wsgi.url_scheme और wsgi.input जैसे CGI और WSGI मान होते हैं। wsgi.input request body पढ़ने का stream है। मौजूद न होने वाले मान, जैसे authentication से पहले REMOTE_USER, छोड़े जा सकते हैं।
def application(environ, start_response):
body = b"Hello from WSGI\n"
start_response(
"200 OK",
[
("Content-Type", "text/plain; charset=utf-8"),
("Content-Length", str(len(body))),
],
)
return [body]यह छोटा उदाहरण contract दिखाता है; इसे production server पर चलाया या सत्यापित नहीं किया गया। WSGI ऐप दो positional arguments लेता है। start_response को status string और (name, value) header pairs की सूची देता है, फिर bytes देने वाला iterable लौटाता है। Python 3 में response body के रूप में str लौटाना नियम के अनुरूप नहीं है। Generator body को हिस्सों में दे सकता है। Iterable में close() हो तो request खत्म होने या client के समय से पहले disconnect करने पर server को इसे कॉल करना चाहिए।
start_response कॉल करने का अर्थ यह नहीं कि headers तुरंत भेजे जाएँ। Server पहले non-empty body bytes या iterable के पूरा होने की प्रतीक्षा कर सकता है। इससे body बनाते समय error आने पर response बदलने की गुंजाइश रहती है। Streaming और बड़ी files पूरी output को memory में रखने से बचाती हैं, लेकिन जल्दी disconnect होने पर iterable पूरा consume न हो; इसलिए resources साफ़ करें।
Framework और server की सीमा
Framework आम तौर पर WSGI callable बनाता है। उदाहरण के लिए Django project का wsgi.py application object देता है जिसे Gunicorn जैसा WSGI server import करता है। असली import path project modules से मेल खाना चाहिए; framework का नाम या development server का पता यह साबित नहीं करता कि सही app load हुई।
तीन भाग अलग-अलग जाँचें:
**Callable:** क्या server दिए गए module path से WSGI object import कर सकता है?
**Request input:** क्या app method, path, query और body को अपेक्षित तरीके से पढ़ती है? Body `wsgi.input` से bytes में पढ़ी जाती है।
**Response output:** क्या app `start_response` से status और headers सेट कर केवल bytes देती है? खाली iterable भी वैध response हो सकता है।
WSGI app-server interface तय करता है; worker संख्या, TLS, restart, host bind या thread model नहीं। ये चुने हुए server, proxy, operating system और service manager पर निर्भर हैं।
WSGI या ASGI चुनें
WSGI synchronous, single-call interface है। यह पारंपरिक request/response apps और कई स्थापित synchronous frameworks के लिए उपयुक्त है। ASGI asynchronous callable application(scope, receive, send) इस्तेमाल करता है और connection जानकारी तथा events का आदान-प्रदान करता है; इसलिए WebSocket जैसे एक connection में अनेक events वाले protocol संभव होते हैं।
| मानदंड | WSGI | ASGI |
|---|---|---|
| App call | Synchronous (environ, start_response) | Asynchronous (scope, receive, send) |
| Response | Bytes का iterable | स्पष्ट event messages का भेजना/पाना |
| आम उपयोग | Synchronous request/response और पुराने apps की संगतता | Async code और WebSocket जैसे multi-event connections |
| Conversion | WSGI server पर सीधा चलना | ASGI server पर चलना; wrapper synchronous WSGI को thread pool में चला सकता है |
थोड़ी async syntax से WSGI app को अपने-आप asynchronous throughput नहीं मिलता। Migration से पहले framework और middleware support, synchronous libraries, लंबे connections और wrapper का execution model जाँचें। ASGI में WSGI compatibility interface translation से मिलती है; इससे app asynchronous code में rewrite नहीं होती।
आम त्रुटियों को पहचानें
**Server `application` import नहीं कर पाता:** project root, module path, callable का नाम और virtual environment dependencies जाँचें।
**Response body का type गलत:** Python 3 WSGI body `bytes` होना चाहिए। Text लौटाने से पहले स्पष्ट रूप से encode करें।
**Headers या error page अप्रत्याशित:** `start_response` call और पहली body byte में अंतर समझें। Response का हिस्सा भेजने के बाद status या headers बदलना कठिन है।
**WebSocket को WSGI app से जोड़ना:** WSGI WebSocket event exchange परिभाषित नहीं करता। समर्थित ASGI path चुनें।
**App path सही पर बाहरी व्यवहार अलग:** WSGI proxy trust, TLS, workers या बिना रुकावट deployment configure नहीं करता; server और deployment अलग जाँचें।
स्रोत और दायरा
[PEP 3333: WSGI 1.0.1](https://peps.python.org/pep-3333/) — callable signature, `environ`, `start_response`, bytes iterable, delayed headers और cleanup
[ASGI परिचय](https://asgi.readthedocs.io/en/stable/introduction.html) — callable, events, multi-event protocols और WSGI compatibility
[ASGI specification](https://asgi.readthedocs.io/en/stable/specs/main.html) — connection scope और receive/send contract
[Django WSGI deployment](https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/) — framework callable और server से जुड़ाव
Specifications और आधिकारिक दस्तावेज़ 2026-11-19 को देखे गए। Code उदाहरण है और चलाया नहीं गया। किसी विशेष server या framework के performance, security या deployment outcome का दावा नहीं है।