I writing a simple application that reads each word from a text file and reports various statistics from about the words. One file reads through every word in the file and sends each word to the server using Python's socket library. This works fine for the first few words before lagging and halting to a stop. When I kill the both the server and the client, I receive this broken pipe error message for the client:
Traceback (most recent call last):
File "client.py", line 27, in <module>
read_file()
File "client.py", line 19, in read_file
s.sendall(message_byte)
BrokenPipeError: [Errno 32] Broken pipe
Here is the code for my client.py:
import socket
import time
host = socket.gethostname()
port = 12345 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
def read_file():
with open('../DataSet/ToTheLighthouse.txt','r') as file:
index = 0
for line in file:
for word in line.split():
word_lower = word.lower()
word_stripped = ''.join([i for i in word_lower if i.isalpha()])
index_str = str(index)
message_byte = word_stripped.encode('utf-8')
s.sendall(message_byte)
time.sleep(1)
data = s.recv(1024)
index+=1
print('Received', repr(data))
read_file()
s.close()
And for the server.py:
import socket
vowel = "aeiou"
host = ''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print(f"Host:{host}\tPort:{port}")
s.listen(1)
conn, addr = s.accept()
print(f'Connected by{addr}')
while True:
try:
data = conn.recv(1024)
if not data:
print("broke")
break
is_boat = lambda string: "boat" in string
starts_with_vowel = lambda char: "char" in vowel
data = str(data)
data = data[1:]
data = ''.join([i for i in data if i.isalpha()])
print(data)
word_len = len(data)
print(f"Word Length:{word_len}\n")
print(f"Has Boat: {is_boat(data)}")
print(f"Vowel Start: {starts_with_vowel(data[0])}")
conn.sendall(b"Server Says:hi")
except socket.error:
print("Error Occured.")
break
conn.close()
I am struggling to see what is causing this problem and if anyone could offer any guidance I'd be very appreciative. Thank you.