Changeset 442
- Timestamp:
- 05/07/08 15:26:10
(8 months ago)
- Author:
- mgoldman
- Message:
Clarifications and modifications per PeBenito?
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r427 |
r442 |
|
| 20 | 20 | client_sock and a (host,port) structure for actions that take place |
|---|
| 21 | 21 | after an accept() """ |
|---|
| 22 | | def __init__(self, host='', port=50000, backlog=5, accept_fun=null_op, |
|---|
| | 22 | def __init__(self, host='', port=50000, backlog=5, accept_func=null_op, |
|---|
| 23 | 23 | info2key=info2ip): |
|---|
| 24 | 24 | self.host = host |
|---|
| … | … | |
| 26 | 26 | self.backlog = backlog |
|---|
| 27 | 27 | self.server = None |
|---|
| | 28 | self.clients = {} |
|---|
| | 29 | self.accept_func = accept_func |
|---|
| | 30 | self.info2key = info2key |
|---|
| 28 | 31 | self.open_socket() |
|---|
| 29 | | self.clients = {} |
|---|
| 30 | | self.accept_fun = accept_fun |
|---|
| 31 | | self.info2key = info2key |
|---|
| 32 | 32 | |
|---|
| 33 | 33 | def __del__(self): |
|---|
| … | … | |
| 37 | 37 | |
|---|
| 38 | 38 | def open_socket(self): |
|---|
| | 39 | ''' Handles opening of the server socket ''' |
|---|
| 39 | 40 | try: |
|---|
| 40 | 41 | self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|---|
| … | … | |
| 55 | 56 | res.append((client, addr)) |
|---|
| 56 | 57 | self.clients[client] = addr |
|---|
| 57 | | self.accept_fun(client, addr) |
|---|
| | 58 | self.accept_func(client, addr) |
|---|
| 58 | 59 | return res |
|---|
| 59 | 60 | |
|---|
| … | … | |
| 64 | 65 | |
|---|
| 65 | 66 | def accept_and_wait(self, timeout = None): |
|---|
| | 67 | ''' Wraps select. If a client tries to connect, service |
|---|
| | 68 | That client and go back to waiting for input from connected |
|---|
| | 69 | clients ''' |
|---|
| 66 | 70 | cl = self.clients.keys() |
|---|
| 67 | 71 | cl.append(self.server) |
|---|
| … | … | |
| 78 | 82 | Accepts all waiting clients, then processes data |
|---|
| 79 | 83 | from clients with input waiting. """ |
|---|
| 80 | | while self.accept_client(0).__len__(): |
|---|
| 81 | | pass |
|---|
| 82 | 84 | for c in self.waiting_clients(0): |
|---|
| 83 | 85 | try: |
|---|
| … | … | |
| 88 | 90 | |
|---|
| 89 | 91 | def get_clients(self): |
|---|
| 90 | | while self.accept_client(0).__len__(): |
|---|
| 91 | | pass |
|---|
| 92 | 92 | return self.clients |
|---|
| 93 | 93 | |
|---|
| 94 | 94 | def shutdown(self): |
|---|
| | 95 | ''' Orderly shutdown all the listening client sockets in prep |
|---|
| | 96 | for exit ''' |
|---|
| 95 | 97 | [c.shutdown(socket.SHUT_RDWR) for c in self.clients.keys()] |
|---|
| 96 | 98 | [c.close() for c in self.clients.keys()] |
|---|
| … | … | |
| 98 | 100 | |
|---|
| 99 | 101 | def send_all(self, msg): |
|---|
| 100 | | print "sending all" |
|---|
| | 102 | ''' Send a message to all clients we know about. ''' |
|---|
| 101 | 103 | [pickle.dump(msg, c.makefile()) for c in self.clients.keys()] |
|---|
| 102 | | print "sent all" |
|---|
| 103 | 104 | |
|---|
| 104 | 105 | def remove(self, socket): |
|---|
| | 106 | ''' remove a client from managment by the server ''' |
|---|
| 105 | 107 | try: |
|---|
| 106 | 108 | print "removal" |
|---|
| … | … | |
| 121 | 123 | l = filter(lambda (c, i): self.info2key(i) == key, self.clients.items()) |
|---|
| 122 | 124 | return [(c, pickle.load(c.makefile())) for (c, i) in l] |
|---|
| 123 | | |
|---|
| 124 | | |
|---|
| 125 | | # class UDPServer: |
|---|
| 126 | | # def __init__(self, host='', port=50000): |
|---|
| 127 | | # self.host = host |
|---|
| 128 | | # self.port = port |
|---|
| 129 | | # self.server = None |
|---|
| 130 | | |
|---|
| 131 | | |
|---|
| 132 | | cont = True |
|---|
| 133 | | |
|---|
| 134 | | def handle_client(client, addr): |
|---|
| 135 | | recvd = client.recv(1024) |
|---|
| 136 | | if(len(recvd) == 0): |
|---|
| 137 | | client.shutdown(socket.SHUT_RDWR) |
|---|
| 138 | | client.close() |
|---|
| 139 | | return |
|---|
| 140 | | if recvd[0:4] == "quit": |
|---|
| 141 | | globals()["cont"] = False |
|---|
| 142 | | return |
|---|
| 143 | | client.send(recvd) |
|---|
| 144 | | |
|---|
| 145 | | if __name__ == "__main__": |
|---|
| 146 | | print "Starting an echo server.\n" |
|---|
| 147 | | s = Server("", 50000) |
|---|
| 148 | | while cont: |
|---|
| 149 | | s.service_clients(handle_client) |
|---|
| 150 | | s.shutdown() |
|---|
Download in other formats:
* Generating other formats may take time.