Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
400 Bad Request
#1
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'
Reply
#2
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
Reply
#3
\n is a *nix new line. Change the request like this:

mysock.send(b"GET / HTTP/1.0\r\n\r\n")
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Please put your code in Python code tags, and error message in error tags. You can find help here.
EDIT:
nevermind :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020