How does the following code snippet work?

How does this work ?

elif path.startswith('/echo'):
    if req[2].startswith("Accept-Encoding"):
        encoding = req[2].split(": ")[1]

when client responses are like this :

> GET /echo/foo HTTP/1.1
> Host: localhost:4221
> User-Agent: curl/7.81.0
> Accept: */*
> Accept-Encoding: gzip  // Client specifies it supports the gzip compression scheme.

Hey @masterfighter999, could you clarify which part of the code you’re trying to understand? Happy to walk through it in more detail.

if req[2].startswith(“Accept-Encoding”):
encoding = req[2].split(": ")[1]

how can req[2] be “Accept-Encoding”?

@masterfighter999 It’s hard to tell without more context.

It depends on how req is being populated. You can try printing the value of req like this:

elif path.startswith('/echo'):
    print(req)
    if req[2].startswith("Accept-Encoding"):
        encoding = req[2].split(": ")[1]