Python Forum
400 Bad Request - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: 400 Bad Request (/thread-6934.html)



400 Bad Request - jimmyjam123 - Dec-14-2017

I am working thru some literature in an effort to learn python on my own. My efforts were progressing favorably until recently when the subject turned to sockets. I have little knowledge in networking but I need help in this area to continue on. The simple listing that I have copied from the book that I am working through looks like this. Also here is the error message:

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

mysock.connect(("www.netbsd.org", 80))

mysock.send(b"GET / HTTP/1.0\n\n")

res = mysock.recv(1024)

while res != b"":
    print(res)
    res = mysock.recv(1024)
---------------------------------------------------------------
b'HTTP/1.1 400 Bad Request\r\nDate: Thu, 14 Dec 2017 11:03:36 GMT\r\nServer: Apache/2.4.27 (Unix)\r\nContent-Length: 226\r\nConnection: close\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>400 Bad Request</title>\n</head><body>\n<h1>Bad Request</h1>\n<p>Your browser sent a request that this server could not understand.<br />\n</p>\n</body></html>\n'


RE: 400 Bad Request - buran - Dec-14-2017

it should be mysock.send(b"GET / HTTP/1.0[color=#ff3333]\r[/color]\n[color=#ff3333]\r[/color]\n")

HTTP 1.0 1.1 expects CRLF


RE: 400 Bad Request - wavic - Dec-14-2017

\n is a *nix new line. Change the request like this:

mysock.send(b"GET / HTTP/1.0\r\n\r\n")


RE: 400 Bad Request - j.crater - Dec-14-2017

Please put your code in Python code tags, and error message in error tags. You can find help here.
EDIT:
nevermind :)