-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketReConnectServer.py
43 lines (38 loc) · 1.38 KB
/
socketReConnectServer.py
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
#! /usr/bin/env python
#-*- coding:utf-8 -*-
# version : Python 2.7.13
# https://github.com/mike-zhang/pyExamples/blob/master/socketRelate/tcpServer1_multithread.py
# https://github.com/mike-zhang/pyExamples/tree/master/socketRelate
import socket
import threading
class ThreadedServer(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
print "listen on port : %d" % self.port
self.sock.listen(5)
while True:
client, address = self.sock.accept()
client.settimeout(60)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
size = 1024
while True:
try:
data = client.recv(size)
if data:
response = data
client.send(response)
print "sendLen: ",len(data)
else:
raise error('Client disconnected')
except:
client.close()
return False
if __name__ == "__main__":
port_num = 12345
ThreadedServer('',port_num).listen()