I am serving a test file as follows:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
facebook = open("sources/facebook.htm","rb")
listen = ("localhost", 2000)
sock.bind(listen)
sock.listen(1)
while True:
connection, client_address = sock.accept()
print("Got a Connection from: " + client_address[0])
sock.send(facebook.read(10000000))
I have downloaded facebook homepage to test connections but when I connect to this page, it gives the following error:
Got a Connection from: 127.0.0.1 Traceback (most recent call last): File "C:\Users\Export.1\blocker.py", line 11, in sock.send(facebook.read(1000000)) OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
How can I fix it?
1 Answer 1
The error occurs because you are sending reply to the server socket instead of client, try changing
sock.send(facebook.read(10000000))
to
connection.send (facebook.read(1000000))
instead