Zach Tusing Zach Tusing - 1 year ago 325
Python Question

TCP Chat server Python 3.4

There is an error in my client side code. The error is,"The operation was attempted on something that is not a socket." how do I go about fixing this error. I also know that my input is not sent to the server yet, If you guys have any tips on how I accomplish that I would also love them. Thanks! The code showed below:

import socket, select, string, sys

def prompt() :
sys.stdout.write('<You> ')

#main function
if __name__ == "__main__":

host = "localhost"
port = 5000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect to remote host
try :
s.connect((host, port))
except :
print('Unable to connect')

print('Connected to remote host. Start sending messages')
input = input()

while 1:
socket_list = [sys.stdin, s]

# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = , [], [])

for sock in read_sockets:
#incoming message from remote server
if sock == s:
data = sock.recv(4096)
if not data :
print('\nDisconnected from chat server')
else :
#print data

#user entered a message
else :
msg = sys.stdin.readline()

Server Code:

import socket, select

#Function to broadcast chat messages to all connected clients
def broadcast_data (sock, message):
#Do not send the message to master socket and the client who has send us the message
for socket in CONNECTION_LIST:
if socket != server_socket and socket != sock :
try :
except :
# broken socket connection may be, chat client pressed ctrl+c for example

if __name__ == "__main__":

# List to keep track of socket descriptors
RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
PORT = 5000

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this has no effect, why ?
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("", PORT))

# Add server socket to the list of readable connections

print("Chat server started on port " + str(PORT))

while 1:
# Get the list sockets which are ready to be read through select
read_sockets,write_sockets,error_sockets =,[],[])

for sock in read_sockets:
#New connection
if sock == server_socket:
# Handle the case in which there is a new connection recieved through server_socket
sockfd, addr = server_socket.accept()
print("Client (%s, %s) connected" % addr)

broadcast_data(sockfd, "[%s:%s] entered room\n" % addr)

#Some incoming message from a client
# Data recieved from client, process it
#In Windows, sometimes when a TCP program closes abruptly,
# a "Connection reset by peer" exception will be thrown
data = sock.recv(RECV_BUFFER)
if data:
broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)

broadcast_data(sock, "Client (%s, %s) is offline" % addr)
print("Client (%s, %s) is offline" % addr)


Answer Source

On Microsoft Windows, python's socket library is implemented using Winsock. Winsock isn't integrated into the operating system like the sockets library on unixy systems. It has its own idea of what a socket is, and does not recognize system pipes or consoles.

On Windows you get the error you see

>>> import platform
>>> print platform.system()
>>> import socket
>>> import select
>>> import sys
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
select.error: (10038, 'An operation was attempted on something that is not a socket')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
select.error: (10038, 'An operation was attempted on something that is not a socket')

On linux it works

>>> import platform                                                                                                                         
>>> print platform.system()
>>> import socket
>>> import select
>>> import sys
([], [], [])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download