1

I have a server in python that listens to GET requests:

host = '127.0.0.1'  # listen to localhost
port = 8001
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(5) # don't queue up any requests

while True:
    csock, caddr = sock.accept()
    print "Connection from: " + repr(caddr)
    req = csock.recv(1024)
    print req

And I get the following request:

Connection from: ('127.0.0.1', 42311)
GET /?categories[]=100&categories[]=200 HTTP/1.1
Host: localhost:8001
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

The requests have the form http://localhost:8000/?categories[]=100&categories[]=200 and I want to get the categories that were passed.

Should I write a regular expression to parse req or I can get 'categories' parameters as attribute parameter of req?

Sergey Ivanov
  • 3,719
  • 7
  • 34
  • 59

1 Answers1

2

It depends on how you intend to use these requests. If you want to respond with HTML pages (or the such) depending on the categories, you should take a look at frameworks like Flask. If you just want to parse the headers, take a look at this. It's a thread on how to parse HTTP headers.

Community
  • 1
  • 1
Lawrence Benson
  • 1,398
  • 1
  • 16
  • 33