38 lines
915 B
Python
Raw Normal View History

2023-07-21 16:51:32 +08:00
import socket
2023-07-26 22:10:10 +08:00
def test_socket_GET():
# 创建一个socket对象
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2023-07-21 16:51:32 +08:00
2023-07-26 22:10:10 +08:00
# 获取服务器的IP地址
# server_ip = socket.gethostbyname('baidu.com')
2023-07-26 22:10:10 +08:00
server_port = 80
2023-07-21 16:51:32 +08:00
2023-07-26 22:10:10 +08:00
# 连接到服务器
s.connect(('pikapython.com', server_port))
2023-07-26 22:10:10 +08:00
# 创建HTTP GET请求
request = 'GET / HTTP/1.1\r\nHost: pikascript.com\r\n\r\n'
2023-07-26 22:10:10 +08:00
# print('request:', request)
s.send(request.encode())
2023-07-21 16:51:32 +08:00
2023-07-26 22:10:10 +08:00
# 接收服务器的响应
response = ''
while True:
try:
recv = s.recv(1024)
except:
break
if not recv:
break
response += recv.decode()
s.close()
return response
2023-07-21 16:51:32 +08:00
2023-07-26 22:10:10 +08:00
for i in range(10):
response = test_socket_GET()
res = 'HTTP/1.1' in response
2023-07-26 22:10:10 +08:00
if res == True:
break
print('test_socket_GET() failed, retrying...')
print('response', response)