简体   繁体   中英

Retrieve data from http using socket libfrary in python

I am using below code to print data from this URL http://data.pr4e.org/romeo.txt

I have tried this code on windows 10.1 system. Is there any modification that I need to escape the (400 Bad Request) result in cmd.

#Code

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org',80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode())

mysock.close()

Output:

C:\Users\Taufique\Desktop\p4e>python socket1.py HTTP/1.1 400 Bad Request Date: Mon, 13 Jul 2020 15:08:08 GMT Server: Apache/2.4.18 (Ubuntu) Content-Length: 308 Connection: close Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at do1.dr-chuck.com Port 80</address>
</body></html>

First, your code currently works for me, even though it is wrong. But many servers try to work around broken clients.

This is your current HTTP request (line breaks added for clarity)

GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n
\r\n

This should be your HTTP request

GET /romeo.txt HTTP/1.0\r\n
Host: data.pr4e.org\r\n
\r\n

For more information please see the standards - that's what standards are for. Don't assume that HTTP is simple because it is all text based. In reality it can be a complex protocol. And wrong code (like yours) might seem to work and break later with the same or other servers if they no longer work around this specific kind of broken code.

I got my mistake. The code is below.

import socket

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org',80))
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode()
mysock.send(cmd)

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

mysock.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM