Skip to content Skip to sidebar Skip to footer

400 Error Header With Sockets

I'm creating a forum status grabber. But I want to use sockets to grab the data from the forum. So I am writing to the socket a header. But there is 400 error. So I made a test scr

Solution 1:

A multi-line Python string adds an extra \n for every line. Note:

>>> s = '''
... Host: rile5.com\r\n
... '''
>>>
>>> s
'\nHost: rile5.com\r\n\n'

There is an extra first line and two \n for each line. This works, but not on the original IP address you used:

import socket
s = socket.socket()
s.connect(("rile5.com", 80))
header = b"""\
GET / HTTP/1.1\r
Host: rile5.com\r
Connection: keep-alive\r
Cache-Control: max-age=0\r
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r
User-Agent: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60\r
Accept-Encoding: gzip, deflate, lzma, sdch\r
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6\r
\r
"""
s.sendall(header)
print(s.recv(10000))

Note the extra slash after the opening quotes. This suppresses the initial newline.

header = b"""\

Also note the extra blank line at the end. This is required so the server knows the header is complete.

Why not just use urllib.request?


Solution 2:

Probably the problem is with the format of your request.

First, your HTTP request starts with a line feed. Also, the lines in a HTTP request must be separated by \r\n, while Python multiline strings only have \n. But since you have literals \r\n in some of them (not all) it is a mess.

Finally, the header must end with an empty line.

My advice is to use a list of strings without any line ending, and then join them:

header_lines = [
 "GET / HTTP/1.1",
 "Host: httn",
 "Connection: keep-alive",
 ...
]

header = "\r\n".join(header_lines) + "\r\n\r\n"

Note that since str.join() does not add a final EOL, you have to add two of them to include the mandatory empty line.


Post a Comment for "400 Error Header With Sockets"