Klaus Demo bjoern / 7bd744d
Merge pull request #107 from yanxurui/master Some minor problems Jonas Haag authored 2 years ago GitHub committed 2 years ago
6 changed file(s) with 16 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
289289 * throw away any previous status and headers. */
290290 Py_CLEAR(request->status);
291291 Py_CLEAR(request->headers);
292 request->state.response_length_unknown = false;
292 request->state.response_length_unknown = true;
293293 }
294294
295295 PyObject* status = NULL;
66
77 for name, size in FILES.items():
88 new_name = '/tmp/bjoern.%s.tmp' % name
9 with open(new_name, 'w') as f:
9 with open(new_name, 'wb') as f:
1010 f.write(os.urandom(size))
1111 FILES[name] = new_name
1212
1313 def app(env, start_response):
1414 start_response('200 ok', [])
1515 if env['PATH_INFO'].startswith('/big'):
16 return open(FILES['big'])
17 return open(FILES['small'])
16 return open(FILES['big'], 'rb')
17 return open(FILES['small'], 'rb')
1818
1919 import bjoern
2020 bjoern.run(app, '0.0.0.0', 8080)
1212 W = len(sys.argv) > 2 and W[sys.argv[2]] or W['filewrapper']
1313
1414 def app(env, start_response):
15 f = open(F)
15 f = open(F, 'rb')
1616 wrapped = W(f, env)
1717 start_response('200 ok', [('Content-Length', str(os.path.getsize(F)))])
1818 return wrapped
2828 'GET / HTTP/1.1\r\nFoo_Bar: bad\r\n\r\n',
2929 'GET / HTTP/1.1\r\nFoo-Bar: good\r\nFoo_Bar: bad\r\n\r\n'
3030 ]
31 conn.send(msgs[int(sys.argv[1])])
31 conn.send(msgs[int(sys.argv[1])].encode())
3232 while 1:
3333 data = conn.recv(100)
3434 if not data: break
35 print repr(data)
36 if data.endswith('0\r\n\r\n'):
35 print(repr(data))
36 if data.endswith(b'0\r\n\r\n'):
3737 if raw_input('new request? Y/n') == 'n':
3838 exit()
39 conn.send('GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n')
39 conn.send(b'GET / HTTP/1.1\r\nConnection: Keep-Alive\r\n\r\n')
99 import threading
1010
1111 if len(sys.argv) > 2:
12 print "usage: %s [host[:port]]" % sys.argv[0]
12 print("usage: %s [host[:port]]" % sys.argv[0])
1313 sys.exit(1)
1414
1515 hostname = 'localhost'
2828 try:
2929 return func(*args, **kwargs)
3030 finally:
31 print '%s took %.2f sec' % (repr(func), time.time() - start)
31 print('%s took %.2f sec' % (repr(func), time.time() - start))
3232 return wrapper
3333
3434 lock = threading.Lock()
4141 s.recv(80)
4242 time.sleep(0.1)
4343 lock.release()
44 print 'release'
44 print('release')
4545 for i in range(10):
4646 s.recv(80)
4747 time.sleep(0.1)
5050 @timed
5151 def time_request():
5252 lock.acquire()
53 print 'acquire'
53 print('acquire')
5454 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5555 s.connect((hostname, port))
5656 s.send('GET /tuple HTTP/1.0' + headers)
66 def start():
77 def return_tuple(environ, start_response):
88 start_response('200 OK', [('Content-Type','text/plain')])
9 print 'tuple'
9 print('tuple')
1010 return (b'Hello,', b" it's me, ", b'Bob!')
1111
1212 def return_huge_answer(environ, start_response):
1414 return [b'x'*(1024*1024)]
1515
1616 def return_404(environ, start_response):
17 start_response('404 Not Found', (('Content-Type','text/plain'), ))
18 return b"URL %s not found" % environ.get('PATH_INFO', 'UNKNOWN')
17 start_response('404 Not Found', [('Content-Type','text/plain')])
18 return b"URL %s not found" % (environ.get('PATH_INFO', 'UNKNOWN').encode())
1919
2020 dispatch = {
2121 '/tuple': return_tuple,