WSGI کو سمجھیں: Python ایپس اور سرورز کے درمیان request-response معاہدہ
WSGI (Web Server Gateway Interface) معیاری call contract ہے جس سے Python web apps اور WSGI servers HTTP requests کا تبادلہ کرتے ہیں۔ ایک app 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 کا تبادلہ کرتے ہیں۔ ایک app 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 تیار کرکے app کو بلاتا ہے۔ Environment میں REQUEST_METHOD، PATH_INFO، QUERY_STRING، wsgi.url_scheme اور wsgi.input جیسی CGI اور WSGI values ہوتی ہیں۔ wsgi.input request body پڑھنے کا stream ہے۔ غیر موجود values، جیسے 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]یہ مختصر مثال معاہدہ سمجھاتی ہے؛ اسے production server پر چلایا یا جانچا نہیں گیا۔ WSGI app دو positional arguments لیتی ہے۔ start_response کو status string اور (نام، قدر) header pairs کی فہرست دیتی ہے، پھر bytes پیدا کرنے والا iterable واپس کرتی ہے۔ Python 3 میں str کو response body کے طور پر لوٹانا معاہدے کے مطابق نہیں۔ 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 مکمل نہ پڑھی جائے؛ 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 count، 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 information اور events کا تبادلہ کرتا ہے، اس لیے ایک connection میں کئی events والے protocols، جیسے WebSocket، سنبھال سکتا ہے۔
| معیار | WSGI | ASGI |
|---|---|---|
| App call | Synchronous (environ, start_response) | Asynchronous (scope, receive, send) |
| Response | Bytes کا iterable | واضح event messages بھیجنا اور وصول کرنا |
| عام استعمال | Synchronous request-response apps اور پرانی compatibility | Async code اور WebSocket جیسے multi-event connections |
| تبدیلی | WSGI server پر براہِ راست چلتا ہے | ASGI server پر چلتا ہے؛ wrapper synchronous WSGI کو thread pool میں چلا سکتا ہے |
تھوڑی async syntax WSGI app کو خودبخود async throughput نہیں دیتی۔ migration سے پہلے framework اور middleware support، synchronous libraries، طویل connections اور wrapper کا execution model دیکھیں۔ ASGI interface translation کے ذریعے WSGI compatibility دیتا ہے، app کو async code میں ازسرنو نہیں لکھتا۔
عام غلطیوں کی پہچان
**Server `application` import نہیں کر سکتا:** project root، module path، callable name اور virtual environment dependencies دیکھیں۔
**Response body کی type غلط:** Python 3 میں WSGI body `bytes` ہونی چاہیے۔ متن واپس کرنے سے پہلے اسے واضح طور پر encode کریں۔
**Headers یا error page غیر متوقع:** `start_response` call اور پہلی body byte میں فرق رکھیں۔ Response کا کچھ حصہ بھیجنے کے بعد status یا headers بدلنا مشکل ہے۔
**WebSocket کو WSGI app سے جوڑنا:** WSGI WebSocket event exchange نہیں بتاتا۔ معاون ASGI راستہ منتخب کریں۔
**App path درست مگر بیرونی رویہ مختلف:** WSGI proxy trust، TLS، workers یا بلا تعطل deployment مقرر نہیں کرتا؛ server اور deployment الگ دیکھیں۔
ذرائع اور دائرہ
[PEP 3333: WSGI 1.0.1](https://peps.python.org/pep-3333/) — callable signature، `environ`، `start_response`، byte iterable، مؤخر 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 معاہدہ
[Django WSGI deployment](https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/) — framework callable اور server سے ربط
Specs اور سرکاری دستاویزات 2026-11-19 کو دیکھی گئیں۔ Code مثال ہے اور چلایا نہیں گیا۔ کسی خاص server یا framework کی کارکردگی، سکیورٹی یا deployment نتیجے کا دعویٰ نہیں۔