Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Ticket #722: cptimeouttest.py

Line 
1 import cherrypy
2 import socket
3
4 fail = True
5
6 def disconnect(*args, **params):
7     global fail
8     if fail:
9         try:
10             fd = cherrypy.request.rfile.rfile.fileno()
11         except AttributeError:
12             fd = cherrypy.request.rfile.fileno()
13         s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
14         s.shutdown(socket.SHUT_WR)
15         s.settimeout(2.0)
16         try:
17             while 1:
18                 data = s.recv(64)
19                 if data == '':
20                     break
21         except socket.timeout:
22             pass
23         fail = False
24         cherrypy.request.process_request_body = False
25
26 cherrypy.tools.disco = cherrypy.Tool('before_request_body', disconnect)
27
28 class Test(object):
29     @cherrypy.expose
30     def index(self):
31         return """\
32 <form action="/echo" method="post">
33     <input name="data" type="text" />
34     <input type="submit" />
35 </form>
36 """
37
38     @cherrypy.expose
39     def ajax(self):
40         global fail
41         fail = True
42         html = """\
43 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
44     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
45
46 <html xmlns="http://www.w3.org/1999/xhtml">
47     <head>
48         <title>POST Test</title>
49         <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
50         <script type="text/javascript">
51             retry = true;
52             function init() {
53                 window.setTimeout(do_req, 5000);
54             };
55
56             function do_req() {
57                 //console.log('init');
58                 xhr = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
59                 xhr.open('POST', '/echo', true);
60                 send_data();
61             };
62
63             function send_data() {
64                 //console.log('send');
65                 var d = "data=it+worked!";
66                 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
67                 xhr.onreadystatechange = function () {
68                     //console.log('state_change');
69                     if (xhr.readyState == 4) {
70                         document.getElementById('container').innerHTML = xhr.responseText;
71                     }
72                 };
73                 xhr.send(d);
74                 
75             };
76             window.onload = init;
77         </script>
78 </head>
79     <body>
80         <div id="container">
81             Initial text!
82         </div>
83     </body>
84 </html>
85 """
86         return html
87
88     @cherrypy.expose
89     def echo(self, data=None):
90         return str(data)
91     echo._cp_config = {'tools.disco.on':True}
92
93 cherrypy.config.update({'server.socket_port':9876})
94 cherrypy.quickstart(Test())
95

Hosted by WebFaction

Log in as guest/cpguest to create tickets