-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_test.py
53 lines (40 loc) · 1.43 KB
/
tcp_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import socket
import time
def tcp_client():
host = "127.0.0.1" # 目标地址
port = 8484 # 目标端口
# 创建 TCP 套接字
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
try:
# 连接到服务器
client_socket.connect((host, port))
print(f"Connected to {host}:{port}")
bytess = client_socket.recv(1024)
print(list(bytess))
message1 = "12345678"
client_socket.sendall(message1.encode())
# 等待 30 秒
print("Waiting 30 seconds...")
time.sleep(30)
# 发送第一个消息
message1 = "hello1"
client_socket.sendall(message1.encode())
print(f"Sent: {message1}")
# 等待 40 秒
print("Waiting 40 seconds...")
time.sleep(40)
# 发送第二个消息
message2 = "hello2"
client_socket.sendall(message2.encode())
print(f"Sent: {message2}")
# 等待 5 秒
print("Waiting 5 seconds...")
time.sleep(5)
# 断开连接
print("Closing connection...")
except ConnectionRefusedError:
print(f"Failed to connect to {host}:{port}. Is the server running?")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
tcp_client()