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 | #!/usr/bin/env python2.6 -tt
# -*- coding: utf-8 -*-
from pprint import pprint
import BaseHTTPServer
import CGIHTTPServer
import random
import sys
import time
class myCGIHTTPRequestHandler (CGIHTTPServer.CGIHTTPRequestHandler):
def _action (self):
r = random.randint (0, 10000000)
if r % 4 == 0:
self.send_error (503)
elif r % 4 == 1:
self.send_error (500)
elif r % 4 == 2:
time.sleep (90)
elif r % 4 == 3:
self.send_response (303, '')
self.send_header ('Location', 'http://help.gadu-gadu.pl/errors/blip/')
self.end_headers ()
def do_GET (self):
self._action ()
def do_POST ():
self._action ()
def do_DELETE ():
self._action ()
def do_PUT ():
self._action ()
def run_while_true (port, server_class = BaseHTTPServer.HTTPServer,
handler_class = myCGIHTTPRequestHandler):
server_address = ('', port)
httpd = server_class (server_address, handler_class)
httpd.serve_forever()
if __name__ == '__main__':
if len (sys.argv) > 0:
port = int (sys.argv[1])
else:
port = 8000
try:
run_while_true (port)
except KeyboardInterrupt:
pass
|