WSGI বোঝা: Python অ্যাপ ও সার্ভারের মধ্যে request-response চুক্তি
WSGI (Web Server Gateway Interface) হলো সেই standard call contract যার মাধ্যমে Python web app ও WSGI server HTTP request আদান-প্রদান করে। একটি app হলো synchronous callable, যা environ এবং startresponse পায়, তারপর response byte-এর iterable ফেরত দেয়। এই সীমাটি বুঝলে framework-এর application object server-এ যুক্ত করা এবং WSGI-কে ASGI থেকে আলাদা করা সহজ হয়। এই নিবন্ধে PEP 3333-এর WSGI 1.0.1 ব্যাখ্যা করে বর্তমান ASGI specification-এর সঙ্গে মূল আচরণ তুলনা করা হয়েছে; এটি কোনো নির্দিষ্ট framework বা server-এর deployment guide নয়।
WSGI (Web Server Gateway Interface) হলো সেই standard call contract যার মাধ্যমে Python web app ও WSGI server HTTP request আদান-প্রদান করে। একটি app হলো synchronous callable, যা environ এবং start_response পায়, তারপর response byte-এর 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 মান থাকে। 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]এই ছোট উদাহরণটি চুক্তি বোঝায়; production server-এ চালানো বা যাচাই করা হয়নি। WSGI app দুটি positional argument পায়। এটি start_response-এ status string ও (নাম, মান) header জোড়ার তালিকা দেয়, তারপর byte তৈরি করা iterable ফেরায়। Python 3-এ response body হিসেবে str ফেরানো চুক্তিসঙ্গত নয়। Generator দিয়ে body ভাগে ভাগে তৈরি করা যায়। Iterable-এ close() থাকলে request শেষ হলে বা client আগেভাগে disconnect করলে server-কে এটি ডাকতে হয়।
start_response কল করলেই header সঙ্গে সঙ্গে পাঠাতে হবে এমন নয়। Server প্রথম non-empty body byte অথবা iterable শেষ হওয়া পর্যন্ত অপেক্ষা করতে পারে। Body তৈরির সময় error হলে response বদলানোর সুযোগ থাকে। Streaming ও বড় file পুরো output memory-তে না রেখেও পাঠাতে পারে; তবে client আগে disconnect করলে iterable শেষ পর্যন্ত পড়া নাও হতে পারে, তাই resource cleanup দরকার।
Framework ও server-এর সীমানা
Framework সাধারণত WSGI callable তৈরি করে। যেমন Django project-এর wsgi.py একটি application object প্রকাশ করে, যা Gunicorn-এর মতো WSGI server import করে। প্রকৃত import path project module-এর সঙ্গে মিলতে হবে; framework-এর নাম বা development server-এর ঠিকানা সঠিক app load হয়েছে প্রমাণ করে না।
তিনটি অংশ আলাদা করে দেখুন:
**Callable:** server কি কনফিগার করা module path থেকে WSGI object import করতে পারে?
**Request input:** app কি method, path, query ও body প্রত্যাশামতো পড়ে? Body `wsgi.input` থেকে byte হিসেবে পড়া হয়।
**Response output:** app কি `start_response` দিয়ে status ও header সেট করে এবং শুধু byte তৈরি করে? খালি iterable-ও বৈধ response হতে পারে।
WSGI app-server interface নির্ধারণ করে; worker সংখ্যা, TLS, restart, host bind বা thread model নয়। এগুলো নির্বাচিত server, proxy, operating system ও service manager-এর ওপর নির্ভর করে।
WSGI না ASGI বেছে নেবেন
WSGI হলো synchronous, এক-কলের interface। এটি প্রচলিত request-response app এবং অনেক প্রতিষ্ঠিত synchronous framework-এর জন্য উপযোগী। ASGI asynchronous callable application(scope, receive, send) ব্যবহার করে connection তথ্য ও event আদান-প্রদান করে; তাই WebSocket-এর মতো এক connection-এ একাধিক event থাকা protocol সমর্থন করতে পারে।
| মানদণ্ড | WSGI | ASGI |
|---|---|---|
| App call | Synchronous (environ, start_response) | Asynchronous (scope, receive, send) |
| Response | Byte iterable | স্পষ্ট event message পাঠানো ও গ্রহণ |
| সাধারণ ব্যবহার | Synchronous request-response ও পুরোনো app compatibility | Async code এবং WebSocket-এর মতো multi-event connection |
| রূপান্তর | WSGI server-এ সরাসরি চলে | ASGI server-এ চলে; wrapper synchronous WSGI-কে thread pool-এ চালাতে পারে |
সামান্য async syntax ব্যবহার করলেই WSGI app স্বয়ংক্রিয়ভাবে async throughput পায় না। স্থানান্তরের আগে framework ও middleware support, synchronous library, দীর্ঘ connection-এর চাহিদা এবং wrapper-এর execution model দেখুন। ASGI interface অনুবাদ করে WSGI compatibility দেয়; app-কে async code হিসেবে নতুন করে লেখে না।
সাধারণ ভুল আলাদা করুন
**Server `application` import করতে পারে না:** project root, module path, callable-এর নাম ও virtual environment dependency দেখুন।
**Response body-এর type ভুল:** Python 3-এর WSGI body `bytes` হতে হবে। Text ফেরানোর আগে স্পষ্টভাবে encode করুন।
**Header বা error page অপ্রত্যাশিত:** `start_response` কল আর প্রথম body byte আলাদা করে দেখুন। Response-এর অংশ পাঠানোর পর status বা header বদলানো কঠিন।
**WebSocket WSGI app-এ পাঠানো:** WSGI WebSocket event exchange নির্ধারণ করে না। সমর্থিত ASGI path বেছে নিন।
**App path ঠিক, কিন্তু বাইরের আচরণ আলাদা:** WSGI proxy trust, TLS, worker বা interruption-free deployment সেট করে না; server ও deployment আলাদাভাবে যাচাই করুন।
উৎস ও পরিসর
[PEP 3333: WSGI 1.0.1](https://peps.python.org/pep-3333/) — callable signature, `environ`, `start_response`, byte iterable, বিলম্বিত header ও cleanup
[ASGI পরিচিতি](https://asgi.readthedocs.io/en/stable/introduction.html) — callable, event, multi-event protocol ও 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-এর সঙ্গে সংযোগ
Specification ও সরকারি নথি 2026-11-19 তারিখে দেখা হয়েছে। Code উদাহরণ, চালানো হয়নি। নির্দিষ্ট server বা framework-এর performance, security বা deployment ফল যাচাইয়ের দাবি নেই।