Python Forum
Problem with Python 3 socket
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Python 3 socket
#1
I need to create a simple server-client interface with Python sockets. This is the code I have:

server.py
sock = socket.socket()
sock.bind((address, port))
sock.listen(5)
client_sock, client_address = self.sock.accept()

while True:
    data = client_socket.recv(1024)
    msg = 'Server got: ' + data.decode()  
    client_socket.sendall(msg.encode())
    client_socket.close()
client.py

		sock = socket.socket()
		sock.connect(server_address)

		sock.sendall(request.encode())
		data = sock.recv(1024)

		while data:
			print(data.decode())
			data = sock.recv(1024)

		sock.close()
But I'm getting the error 'OSError: [WinError 10038] An operation was attempted on something that is not a socket' on the server side.

I want to read whatever is in the stream and them close it.
Keep it simple, stupid — kiss principle.
Reply
#2
I'm surprised that's the error you get, because I see a whole lot of undefined variables.
Reply
#3
Please post the whole code and not just parts.
For me it looks like copy&pasted code from other sources.
For example in your server.py:
sock = socket.socket()
...
client_sock, client_address = self.sock.accept()
self.sock.accept is inside a class, but you assigned before to sock.

So post your whole code, that we can copy it and get a similar StackTrace.
Don't be shy :-D
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
SOLVED. All I needed was a sock.shutdown(socket.SHUT_WR) after the sock.sendall(...) in the client. Now the client sends all that and tell the server it's over, then the server can close its end properly and all is good.
Keep it simple, stupid — kiss principle.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020