0Pricing
Flask Academy · บทเรียน

ส่วนหัว คุกกี้ และ IP ของไคลเอนต์

เข้าถึง request.headers คุกกี้ และ remote_addr

ส่วนหัว คุกกี้ และ IP ของไคลเอนต์ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ของไคลเอนต์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ส่วนหัว คุกกี้ และ IP ของไคลเอนต์”

เข้าถึง request.headers คุกกี้ และ remote_addr คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ส่วนหัว คุกกี้ และ IP ของไคลเอนต์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. สตริงคำค้นผ่าน request.args
  2. ฟิลด์แบบฟอร์มผ่าน request.form
  3. เนื้อหา JSON ผ่าน request.get_json
  4. ส่วนหัว คุกกี้ และ IP ของไคลเอนต์
← กลับไปที่ Flask Academy