Yair Haber
Yair Haber

Reputation: 19

How to make a server that is always listening with socket?

iv'e build a basic command server:

import socket
import time
import random

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8821))

server_socket.listen(1)

(client_socket, client_address) = server_socket.accept()

client_input = client_socket.recv(1024)
client_input = client_input.upper()


if client_input == 'TIME':
    client_socket.send(time.strftime("%c"))
elif client_input == 'RAND':
    client_socket.send(str(random.randrange(0, 11)))
elif client_input == 'NAME':
    client_socket.send("My master called me \"Arik\". Funny, ha?")
else:
    client_socket.send("Unknown command")

if len(client_input) > 4 or len(client_input) < 4:
    client_socket.send("The length of your messege\nneeds to be 4 chracters.\nI know only 4 commands.\nRAND, TIME, NAME & EXIT.\nThanks.")


client_socket.close()
server_socket.close()

and a basic client:

import socket
my_socket = socket.socket()
my_socket.connect(('127.0.0.1', 8820))

message = raw_input("Insert Command\n")
my_socket.send(message)

data = my_socket.recv(1024)
print "Answer Sent: " + data

my_socket.close()

now i'd like to make the server listen to command all the time, until he will get an "exit" command. I didn't quite got the way of doing that, so i'd like you to help me. thanks a lot!

Upvotes: 0

Views: 4871

Answers (2)

Mohd
Mohd

Reputation: 5613

You can simply run a loop to keep receiving from client until 'exit' is received:

import socket
import time
import random

server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 8821))

server_socket.listen(1)

(client_socket, client_address) = server_socket.accept()

while True:
    client_input = client_socket.recv(1024).upper()
    if len(client_input) > 4 or len(client_input) < 4:
        client_socket.send("The length of your messege\nneeds to be 4 chracters.\nI know only 4 commands.\nRAND, TIME, NAME & EXIT.\nThanks.")
    else:
        if client_input == 'TIME':
            client_socket.send(time.strftime("%c"))
        elif client_input == 'RAND':
            client_socket.send(str(random.randrange(0, 11)))
        elif client_input == 'NAME':
            client_socket.send("My master called me \"Arik\". Funny, ha?")
        elif client_input == 'EXIT':
            client_socket.send("Exiting.")
            break
        else:
            client_socket.send("Unknown command")

client_socket.close()
server_socket.close()

And your client should be:

import socket
my_socket = socket.socket()
my_socket.connect(('127.0.0.1', 8820))

message = ''
while message.upper() != 'EXIT':
    message = raw_input("Insert Command\n")
    my_socket.send(message)
    data = my_socket.recv(1024)
    print "Answer Sent: " + data

my_socket.close()

Upvotes: 4

Lalit Som
Lalit Som

Reputation: 1

I have never used python networking myself, but I think this would work. You can just put your server listener in a loop and if it receive EXIT then break the loop.

while(true):
    server_socket.listen(1)

    (client_socket, client_address) = server_socket.accept()

    client_input = client_socket.recv(1024)
    client_input = client_input.upper()


    if client_input == 'TIME':
        client_socket.send(time.strftime("%c"))
    elif client_input == 'RAND':
        client_socket.send(str(random.randrange(0, 11)))
    elif client_input == 'NAME':
        client_socket.send("My master called me \"Arik\". Funny, ha?")
    elif client_input == 'EXIT':
          break;
    else:
        client_socket.send("Unknown command")

    if len(client_input) > 4 or len(client_input) < 4:
        client_socket.send("The length of your messege\nneeds to be 4 chracters.\nI know only 4 commands.\nRAND, TIME, NAME & EXIT.\nThanks.")

client_socket.close()
server_socket.close()

Upvotes: -1

Related Questions