0

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.

Keegan Husom
  • 139
  • 1
  • 8
  • Show the error with traceback please. And the file you are trying to push. – Mad Physicist Nov 12 '18 at 20:18
  • lol the file is alice and wonderland in in a txt file. The trace back is: Traceback (most recent call last): File "webserver.py", line 53, in main() File "webserver.py", line 35, in main conn.sendall(l) socket.error: [Errno 104] Connection reset by peer – Keegan Husom Nov 12 '18 at 20:20
  • Post that in your question please. You can't format a comment. – Mad Physicist Nov 12 '18 at 20:23
  • Updated the Question – Keegan Husom Nov 12 '18 at 20:26
  • Possible duplicate of [Python handling socket.error: \[Errno 104\] Connection reset by peer](https://stackoverflow.com/questions/20568216/python-handling-socket-error-errno-104-connection-reset-by-peer) – Omer Ben Haim Nov 12 '18 at 20:31

0 Answers0