-
2 weeks 2 days
-
2 weeks 4 days
-
4 weeks 2 days
-
4 weeks 5 days
-
4 weeks 6 days
Python threads part 2
I had a quite busy weekend so I wasn't able to post yesterday... anyway, here's the second part of Python thread. On the part 1, we left off by showing the server code and explaining roughly how it works so today its all about the client side and then I'm going to show you how it works.
from socket import *
import sys, thread
def process_server(client):
global closing
while True:
response = client.recv(1024)
if response == "close": break
print response
closing = True
if __name__ == "__main__":
closing = False
if len(sys.argv) != 3:
print "Wrong number of arguments"
sys.exit(1)
host = sys.argv[1]
port = int(sys.argv[2])
client = socket(AF_INET, SOCK_STREAM)
client.connect((host, port))
thread.start_new_thread(process_server, (client, ))
while True:
request = raw_input()
if closing: break
client.send(request)
if request == "close": break
client.close()
The client has only 1 function and a main, the function process_server is the one responsible for accepting any incoming messages from the server. First, it brings into scope the global variable closing, the purpose of closing variable is to let the client know that for some reason the server is closing its connection. Then we have a loop that reads 1024 bytes of data from the server, if the message is close then break out of the loop and set closing to True.
The main just checks for valid number of arguments, initializes a host and a port then spawn a new thread to handle any incoming messages from the server using the process_server function. Since the server is handled on a separate thread, we can use the main thread (The one that's running the client) to handle any incoming input form the client. Hence, making the client still responsive.
Let me show you how this works, first we start the server:
marconi$ python server.py 2000
then we connect the client:
marconi$ python client.py 127.0.0.1 2000 Howdy, stranger!
Once client is connected, we can then execute some commands:
list
{'price': 0.5, 'name': 'Siomai'}
{'price': 1.5, 'name': 'Siopao'}
{'price': 0.0, 'name': 'Sagmaw'}
add Kikiam 3.0
{'price': 0.5, 'name': 'Siomai'}
{'price': 1.5, 'name': 'Siopao'}
{'price': 0.0, 'name': 'Sagmaw'}
{'price': 3.0, 'name': 'Kikiam'}
delete Siopao
{'price': 0.5, 'name': 'Siomai'}
{'price': 0.0, 'name': 'Sagmaw'}
{'price': 3.0, 'name': 'Kikiam'}
delete
Invalid command
add
Invalid command
On the server, we can also execute some commands:
>> list
{'price': 0.5, 'name': 'Siomai'}
{'price': 0.0, 'name': 'Sagmaw'}
{'price': 3.0, 'name': 'Kikiam'}
>> clients
[]
The clients command spits the list of clients connected, here we can see the raw format of the socket. The third command in the server is to send messages to clients:
>> send 0 You done yet?
We assume that the admin on the server knows that the list in python starts at zero, so sending message to the first client needs to have an index of zero. The client then receives the message:
You done yet?
In our example above we only use one client but you can connect as many clients as you want and try to have them interact with the server simultaneously. I hope you learned something and if you have any questions just post them as a comment and I'll try to answer them.

Facebook
Twitter
Post new comment