This is the code:
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('google.com', 80))
print(mysock)
cmd = 'GET http://data.py4e.org/romeo.txt http:/1.0\n\n'.encode()
mysock.send(cmd)
while True:
data = mysock.recv(512)
if (len(data) < 1):
break
print(data.decode())
mysock.close
It is supposed to create a simple python web browser, but it is not working. It was copied directly out of an instructional video. The complete return from it is this:
"D:\Tools\Coding\PyCharm Community Edition 2021.2.3\bin\pythonProject2\venv\Scripts\python.exe" "D:/Tools/Coding/PyCharm Community Edition 2021.2.3/bin/pythonProject2/NP.py"
<socket.socket fd=360, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('10.0.0.216', 54333), raddr=('142.250.217.78', 80)>
HTTP/1.0 400 Bad Request
Content-Type: text/html; charset=UTF-8
Referrer-Policy: no-referrer
Content-Length: 1555
Date: Fri, 28 Jan 2022 01:28:58 GMT
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 400 (Bad Request)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:
180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:u
rl(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
</style>
<a href=//www.google.com/><span id=
logo aria-label=Google></span></a>
<p><b>400.</b> <ins>That’s an error.</ins>
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>
Process finished with exit code 0
CodePudding user response:
The server is correct. What you do has some similarities to a HTTP request but isn't actually one. There are actually many things wrong with it.
mysock.connect(('google.com', 80))
cmd = 'GET http://data.py4e.org/romeo.txt http:/1.0\n\n'.encode()
The request should be
GET /romeo.txt HTTP/1.0\r\nHost: data.py4e.org\r\n\r\n
Note the added Host header, the different line ends, the different specification of the protocol, the different path (path only not full URL). Apart from that the request must be send to the actual server for the domain (data.py4e.org) and not to google.com.
Note that HTTP is a more complex protocol than it looks like. Therefore I recommend to use established libraries like requests instead. If you really want to implement everything by your own please don't guess the protocol as you currently do, but study the actual standard. That's what standards are for. At the minimum study the wikipedia article about HTTP which provides already lots more information than you currently have.
CodePudding user response:
As an assumption, I think your problem is the requests going through could be literally illegal. When I made a browser I did not use socket and did not have that problem so I recommend doing something different. Try This: https://devtut.github.io/python/webbrowser-module.html
