I'm having trouble getting my server to work. I'm trying to push a txt file to the local client. I keep getting an error with connection reset by peer. How do I fix this? Here is my code that I have.
import socket
from socket import *
from datetime import datetime
import sys
s = socket(AF_INET, SOCK_STREAM)
HOST = "127.0.0.2" # Local client is going to be 127.0.0.1
PORT = 4300 # Open http://127.0.0.2:4300 in a browser
LOGFILE = "webserver.log"
def main():
"""Main loop"""
with socket(AF_INET, SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
print('Listening on port {}'.format(PORT))
conn, addr = s.accept()
print("Waiting for client response")
with conn:
while True:
data = conn.recv(1024)
dt = data.decode()
print(dt)
if not data:
print('Connection closed')
break
if data:
filename = 'alice30.txt'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.sendall(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
else:
print("Invalid")
conn.sendall("Invalid Request".encode())
print("Waiting for client response")
if __name__ == "__main__":
main()
The Traceback I get is:
Traceback (most recent call last):
File "webserver.py", line 53, in <module>
main()
File "webserver.py", line 24, in main
data = conn.recv(1024)
socket.error: [Errno 104] Connection reset by peer
I believe this isn't the same issue that the linked to duplicate. In the answers they said that is is the issue of the server. My code is the server... I think it has to deal with the bytes being sent back and forth.