TypeError:在尝试将输入放入web浏览器时,需要类似byteslike的对象,而不是“元组”

2024-05-03 05:39:03 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中,我试图使代码出现在已输入的URL中。它只是提出了不好的要求。这也是用python制作的

我的代码是:

import socket

print("Enter a URL Below:")
x = input()


mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET', x, '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()
print('If it says "HTTP/1.1 200 OK" at the top, That means the connections are OK. The code is ')

我的输出:

Enter a URL Below:
https://www.google.com/webhp?hl=en&sa=X&ved=0ahUKEwjU8fr9oJDoAhVB7qwKHX6VB0AQPAgH
HTTP/1.1 400 Bad Request

Date: Tue, 10 Mar 2020 15:50:00 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>
If it says "HTTP/1.1 200 OK" at the top, That means the connections are OK. The code is 
>>> 
= RESTART: C:/Users/sharm/AppData/Local/Programs/Python/Python38-32/WebBrowser.py
Enter a URL Below:
https://www.google.com/
Traceback (most recent call last):
  File "C:/Users/sharm/AppData/Local/Programs/Python/Python38-32/WebBrowser.py", line 10, in <module>
    mysock.send(cmd)
TypeError: a bytes-like object is required, not 'tuple'

我只想用Python制作一个Web浏览器,GitHub上的浏览器是一个包,我不喜欢这样

给我Python 3代码来修复它


Tags: the代码cmdcomhttpurldatais