Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python socket programming OSError: [WinError 10038] an operation was attempted on something that is not a socket

Tags:

python

sockets

I am working on this code

from socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)

while 1:
    print ("waiting on connection")
    clientsock, addr = serversock.accept()
    print ('connected from:', addr)
    while 1:
        data = clientsock.recv(1024).decode()
        if not data: break 
        clientsock.send(data.encode())
        clientsock.close()

serversock.close()

I get this error:

OSError: [WinError 10038] an operation was attempted on something that is not a socket
like image 659
user2133251 Avatar asked Mar 04 '13 20:03

user2133251


2 Answers

You are closing the clientsock after reading only part of the data.

clientsock.close()

is at the wrong level of indentation. Move it to the left by one step.

like image 100
Robᵩ Avatar answered Oct 15 '22 15:10

Robᵩ


The problem is with close(). after connection get close you need to initialize the connection again

add this line inside while loop

serversock = socket(AF_INET, SOCK_STREAM)
like image 26
zad fab Avatar answered Oct 15 '22 16:10

zad fab