mardi 4 août 2015

Writing a server socket on python

So my server_socket looks like this:

import socket
import time
import random

server_socket = socket.socket()
server_socket.bind(('127.0.0.1',8820))
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
data = client_socket.recv(1024)
while data != 'exit':
    if data == 'time':
        client_socket.send(time.asctime(time.localtime()))
    elif data == 'rand':
        client_socket.send(str(random.randrange(1,11)))
    elif data == 'name':
        client_socket.send('Super Server')
    else:
        client_socket.send('Your request is unavailable. Please choose time, rand, name or exit')
    data = ""
    data = client_socket.recv(1024)
client_socket.send('Thank u for contacting us, the connection will be close.')
client_socket.close()

As you can see, the server and the client are in my computer. What I am trying to do here, is when a person send to my server the word 'time' he will get the specific time, etc', until the person type 'exit' and then the server close his connection but not the server connection ofcourse. My code is running a problem with the if and elif statements. Because I am expecting from my client (which it's code is not relevent) to send multiple request and the final one will be 'exit'. As u can see, my problem is actually to check the info that the client sent. What should I do? Thank you :)

Aucun commentaire:

Enregistrer un commentaire