-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0tut55ThreadServer.py
More file actions
43 lines (33 loc) · 1.06 KB
/
0tut55ThreadServer.py
File metadata and controls
43 lines (33 loc) · 1.06 KB
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
import SocketServer
import threading
import time
#creo mi TCP Handler
class MiTcpHandler(SocketServer.BaseRequestHandler):
#sobrescribo la funcion handle
def handle(self):
data= ""
while data != "salir":
#intento recibir informacion
try:
data= self.request.recv(1024)
print data
time.sleep(0.1) #espero 0.1 segundos antes de leer neuvamente
#si hubo un error lo digo y termino el handle
except:
print "El cliente D/C o hubo un error"
data="salir"
#no se assusten Creo una clase llamada ThreadServer
class ThreadServer (SocketServer.ThreadingMixIn, SocketServer.ForkingTCPServer):
pass
def main():
#host & port
host ="localhost"
port= 9999
#creo el server
server = ThreadServer((host,port),MiTcpHandler)
#creo un thread del server
server_thread = threading.Thread(target=server.serve_forever)
#empiezo el thread
server_thread.start()
print "server corriendo.."
main()