The following four programs provide an example of how the z_service module can be used. This recipe demonstrates a chatting program built off of the easy to use framework provided by the Client, Server, and Sync_Server classes. These abstractions are meant to make networked function calls intuitive and easy to set up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #==============================================================================#
Server.py
#==============================================================================#
import time
class Server:
def __init__(self, max_age):
self.__max_age = max_age * 60
self.__message = {}
self.__ticket = 1
def add(self, ID, message):
self.__message[self.__ticket] = time.clock(), ID, message
self.__ticket += 1
def query(self, ID, ticket):
if not self.__message:
return 0, []
minimum = min(self.__message)
if ticket < minimum:
ticket = minimum
messages = []
for index in range(ticket, self.__ticket):
clock, message_ID, message = self.__message[index]
if message_ID != ID:
messages.append('%s: %s' % (message_ID, message))
return self.__ticket, messages
def serve(self):
while True:
if not self.__message:
time.sleep(self.__max_age)
else:
time.sleep(self.__max_age + self.__message[min(self.__message)][0] - time.clock())
del self.__message[min(self.__message)]
import z_service
def main():
chat_server = Server(5)
rpc_server = z_service.Sync_Server('', 9000)
rpc_server.add_service('add', chat_server.add)
rpc_server.add_service('query', chat_server.query)
chat_server.serve()
if __name__ == '__main__':
main()
#==============================================================================#
Input.py
#==============================================================================#
import z_service
def main():
name = raw_input('Name: ')
client = z_service.Client(raw_input('Host: '), 9000)
while True:
client('add', name, raw_input('Say: '))
if __name__ == '__main__':
main()
#==============================================================================#
Output.py
#==============================================================================#
import z_service, time
def main():
name = raw_input('Name: ')
client = z_service.Client(raw_input('Host: '), 9000)
ticket = 0
while True:
temp_ticket, messages = client('query', name, ticket)
if temp_ticket:
ticket = temp_ticket
for message in messages:
print message
time.sleep(5)
if __name__ == '__main__':
main()
#==============================================================================#
Logger.py
#==============================================================================#
import z_service, time
def main():
log = file(raw_input('Filename: '), 'w')
client = z_service.Client(raw_input('Host: '), 9000)
ticket = 0
while True:
temp_ticket, messages = client('query', None, ticket)
if temp_ticket:
ticket = temp_ticket
for message in messages:
log.write(message + '\n')
time.sleep(60 * 4)
if __name__ == '__main__':
main()
|
Server.py should be started first. Input.py should be started to post messages to the server. Output.py should be started to receive messages from the server. Logger.py should be started to log the conversation.