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!
socketmodule example. Basically, just run it in a loop and keep runningaccept()on the socket server andrecv()on the socket.