-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathclient_socket.py
More file actions
34 lines (29 loc) · 1.07 KB
/
Copy pathclient_socket.py
File metadata and controls
34 lines (29 loc) · 1.07 KB
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 = 'localhost'
PORT = 53091
write_index = 0
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.connect((HOST, PORT))
except OSError as err:
print("Client socket failed to open with error: {0}".format(err))
while write_index < 10:
string = "Writing data at index: {0} ".format(write_index)
s.sendall(str.encode(string))
# Block and write up to 512 bytes to the write buffer.
data = s.recv(512)
# Using bpo-32373, check to see if this socket is blocking.
if s.getblocking():
print("(Blocking socket) " + data.decode())
else:
print("(Non-blocking socket) " + data.decode())
write_index += 1
# Close the socket, but wait.... is it really closed???
s.close()
# Just to make sure (bpo-32454), send data to validate that the
# socket is closed.
try:
s.sendall(str.encode('closed'))
print("Client socket remains open")
except OSError as err:
print("Client socket is closed with error: {0}".format(err))