forked from cherrypy/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsClientConnected
51 lines (43 loc) · 1.35 KB
/
IsClientConnected
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
Sometimes it may be useful to continue processing the request only if client is still connected.
Example:
Enter server address in the browser. Within 20 seconds, close the browser window. In CherryPy server log you should see info about that.
The function isClientConnected tested on Windows Server only.
{{{
import time
import logging
import cherrypy
def isClientConnected(asock=None):
if asock is None:
asock=cherrypy.request.rfile.rfile._sock
ret=0
atimeout=asock.gettimeout()
try:
asock.settimeout(0.001)
try:
adata=asock.recv(0)
except:
ret=1
finally:
asock.settimeout(atimeout)
return ret
class Test:
rcnt=0
@cherrypy.expose
def index(self):
self.rcnt=self.rcnt+1
acnt=self.rcnt
for i in range(20):
time.sleep(1)
if isClientConnected():
cherrypy.log('timer: %d - %d'%(acnt,i),context='',severity=logging.DEBUG,traceback=False)
else:
cherrypy.log('timer: %d - disconnected'%(acnt,),context='',severity=logging.DEBUG,traceback=False)
return 'Disconnected - no need to wait more...'
return 'OK'
if __name__ == '__main__':
cherrypy.config.update({
'server.socket_host':'0.0.0.0',
'server.socket_port':9092,
})
cherrypy.quickstart(Test())
}}}