-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysocket.py
More file actions
34 lines (28 loc) · 762 Bytes
/
mysocket.py
File metadata and controls
34 lines (28 loc) · 762 Bytes
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
import socket
HOST = "127.0.0.1"
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
with conn:
print('connected')
while True:
data = conn.recv(1024)
print(f'data = {data}')
conn.sendall(b"hi back")
print('finished')
import socket
HOST = "127.0.0.1"
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"hi 1")
data=s.recv(1024)
print(f"Received {data!r}")
s.sendall(b"hi 2")
data=s.recv(1024)
print(f"Received {data!r}")
s.sendall(b"hi 3")
data=s.recv(1024)
print(f"Received {data!r}")
print('client closed')