0Pricing
Flask Academy · درس

الترويسات وملفات تعريف الارتباط وعنوان IP للعميل

الوصول إلى request.headers وcookies وremote_addr.

الترويسات وملفات تعريف الارتباط وعنوان IP للعميل درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Requests Carry Metadata

Beyond the body, every request ships metadata like headers, cookies, and the client address that your views can inspect.

Read Headers With request.headers

The request.headers object is a case-insensitive mapping of all HTTP headers the client sent along with the call.

agent = request.headers["User-Agent"]

Headers Are Case-Insensitive

You can read a header as content-type or Content-Type and get the same value, since header names ignore case by design.

ct = request.headers.get("Content-Type")

Get the Authorization Header

API clients often send credentials in the Authorization header, which you read like any other header value.

auth = request.headers.get("Authorization")

Cookies Live in request.cookies

Values your server set earlier come back in request.cookies, a dict-like object keyed by each cookie name.

theme = request.cookies.get("theme")

Always Default Your Cookies

A first-time visitor has no cookies, so use .get with a sensible default to avoid surprises on their initial request.

theme = request.cookies.get("theme", "light")

The Client IP via remote_addr

Flask exposes the connecting address as request.remote_addr, handy for logging or simple rate-limit bookkeeping.

ip = request.remote_addr

Proxies Hide the Real IP

Behind a proxy, remote_addr is the proxy address. The true client IP often sits in the X-Forwarded-For header instead.

ip = request.headers.get("X-Forwarded-For")

Know the Requested URL

Inspect request.url for the full address, or request.path for just the route portion the client asked for.

where = request.path

Identify the HTTP Method

Check request.method to see whether this call is a GET, POST, or another verb, then branch your logic accordingly.

if request.method == "POST":
    pass

A Diagnostic whoami View

Combine these and a whoami route can report the caller IP and browser, perfect for debugging real traffic.

@app.route("/whoami")
def whoami():
    return f"{request.remote_addr}"

Quick Check

Test what you learned about request metadata.

Recap: Headers and Cookies

You read headers and cookies safely, found the client IP via remote_addr, and learned proxies hide the real address. 🚀

الأسئلة الشائعة

هل درس «الترويسات وملفات تعريف الارتباط وعنوان IP للعميل» مجاني؟

نعم — نص درس «الترويسات وملفات تعريف الارتباط وعنوان IP للعميل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «الترويسات وملفات تعريف الارتباط وعنوان IP للعميل»؟

الوصول إلى request.headers وcookies وremote_addr. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «الترويسات وملفات تعريف الارتباط وعنوان IP للعميل»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. سلاسل الاستعلام عبر request.args
  2. حقول النموذج عبر request.form
  3. أجسام JSON عبر request.get_json
  4. الترويسات وملفات تعريف الارتباط وعنوان IP للعميل
← العودة إلى Flask Academy