通过套接字Python发送GET请求

时间:2020-04-24 14:07:55

标签: get serversocket python-3.8

我正在尝试将GET请求发送到使用Python 3.8.2的服务器。但是,当我执行命令print(sock.recv(1024))时,它只返回给我的指令:

b'\n Iniate communication with GET to retrieve the encrypted messages.\n\n Then return the messages decrypted to the server, taking care to ensure each message is split on to a newline\n '

这是我的代码:

#
# Connect over TCP to the following server 'localhost', 10000
# Initiate communication with GET to retrieve the encrypted messages.
# Then return the messages decrypted to the server,
# taking care to ensure each message is split on to a newline
#


import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", 10000))
sock.send(b"GET / HTTP/1.1\r\nHost:127.0.0.1\r\n\r\n")
print(sock.recv(1024))

GET请求有问题吗?

1 个答案:

答案 0 :(得分:0)

回答我的问题,仅用sock.send(b"GET / HTTP/1.1\r\nHost:127.0.0.1\r\n\r\n")代替行sock.send(b"GET")就可以了。