Sam Tubb
Sam Tubb

Reputation: 965

Python: Notification System?

I am working on a chat server that runs on my local network using socket, and then I have a client program running on all of the computers in my house, and this program allows all of the clients to talk to each other.

The problem is, you have to manually update the chat log by pressing enter.

The way I want it to work, maybe, is to check for a new message every few seconds, and if there is a new one, play a sound. Does anyone know how I can do this, I'll try to figure it out on my own, as I have done with most of this project, but any help is appreciated.

Here is the server:

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('192.168.1.80', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
sock.listen(1)
print 'Waiting for user...'
convo='Welcome!'
while True:
    # Find connections
    connection, client_address = sock.accept()
    try:
        data = connection.recv(999)
        if data=='EMPTY':
            pass
        else:
            print data
            convo=convo+'\n'+data
        connection.sendall(convo)
    except:
        connection.close()

Here is the client:

import socket
import sys,os

name=raw_input("Enter name: ")
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('192.168.1.80', 10000)
print >>sys.stderr, 'connecting to %s port %s' % server_address
while True:
    message=raw_input('Message: ')
    try:
        os.system('cls')
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect(server_address)
        if message is not '':
            sock.sendall(name+": "+message)
        else:
            sock.sendall('EMPTY')
        if message=='quit':
            break
        x=sock.recv(999)
        print x
    except:
        break
sock.close()

Thanks!

Upvotes: 1

Views: 1735

Answers (2)

voodoogiant
voodoogiant

Reputation: 2158

Here's question for playing audio in python: Play audio with Python

As for your client, why are you reconnecting to your server every single time? Anyway, if I understand the problem correctly you're blocking on user input, but also want to handle messages from the server.

Without getting complicated with threads, I would recommended using a recurring signal, which I believe could handle this. There's a function call setitimer(), which will break what you're doing and call a function every so often then return to where you were (user input). In your timer function, check for server messages, print any received, play your sound and return to user input. There's an setitimer() example enter link description here.

Might be a little ugly with the user typing, so you may need to reprint what they're currently typing, but haven't sent out (using something other than raw_input()).

For a slightly more complicated option, which may help you there's a function call select(), which can block while listening for socket input AND user input. Then you just distinguish which is which and keep it all in one loop.

while True:
    # select on server socket and user input (blocks for either one)

    # if here, either a message has been received or the user typed something

    # if a message from server
        ...
        playSound()
    # else 
        # send to server

Upvotes: 1

shx2
shx2

Reputation: 64358

If you need two operations to happen at the same time (the client script needs to read input from the user and read new messages from the server), then you'd need to either use threads (one thread for reading user input, and one for reading messages from the server), or futures (since python3.2).

Upvotes: 1

Related Questions