Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

chat server/client in python

Tags:

python

sockets

I using this chat server/client I'm getting a problem when trying to run the client side I'm using python 3.4 under windows for that line

ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])

WinError 10038 An operation was attempted on something that is not socket

When I change this line socket_list = [sys.stdin,s] to socket_list = [s] then the error disappear but the program then not wait for input from client

any idea ?

this is the code

# chat_client.py

import sys
import socket
import select

def chat_client():
    #if(len(sys.argv) < 3) :
      #  print ('Usage : python chat_client.py hostname port')
      #  sys.exit()

    host = 'localhost'
    port = 10000

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

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

    print ('Connected to remote host. You can start sending messages')
    sys.stdout.write('[Me] '); sys.stdout.flush()
    prompt()
    while 1:
        socket_list = [sys.stdin,s]

        # Get the list sockets which are readable
        ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])

        for sock in ready_to_read:
            print('tset')
            if sock == s:
                # incoming message from remote server, s
                data = sock.recv(4096)
                if not data :
                    print ('\nDisconnected from chat server')
                    sys.exit()
                else :
                    #print data
                    sys.stdout.write(data)
                    sys.stdout.write('[Me] '); sys.stdout.flush()     

            else:
                print('tset')
                # user entered a message
                #msg= input()
                msg = sys.stdin.readline()
                s.send(msg)
                sys.stdout.write('[Me] '); sys.stdout.flush() 

if __name__ == "__main__":

    sys.exit(chat_client())
like image 278
Adam Bahrani Avatar asked Sep 20 '26 03:09

Adam Bahrani


1 Answers

Quote from the select doc

Note that on Windows, it only works for sockets; on other operating systems, it also works for other file types (in particular, on Unix, it works on pipes). It cannot be used on regular files to determine whether a file has grown since it was last read.

Maybe the problem come from the limitation on windows.

EDIT :

This confirm the problem on windows:

select.select(rlist, wlist, xlist[, timeout])

Empty sequences are allowed, but acceptance of three empty sequences is platform-dependent. (It is known to work on Unix but not on Windows.)

You should see the Note section of the doc that provide a workaround with WinSock Library : https://docs.python.org/2/library/select.html?highlight=select#select.select

like image 94
Fumbo Avatar answered Sep 22 '26 15:09

Fumbo