0

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!

1
  • Check the socket module example. Basically, just run it in a loop and keep running accept() on the socket server and recv() on the socket. Commented Sep 3, 2017 at 17:01

2 Answers 2

4

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() 
Sign up to request clarification or add additional context in comments.

3 Comments

Glad to help, feel free to accept the answer if you find it useful :-)
File "/Networks/EX2.6---server.py", line 13, in <module> client_input = client_socket.recv(1024).upper() socket.error: [Errno 54] Connection reset by peer
Because your client is disconnecting before sending 'exit', I just edited the answer to add a fix for that.
-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() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.