def Handle(client_socket, addr): print >> sys.stderr, 'connection from %r' % (addr, ) f = client_socket.makefile() # Read HTTP request. line1 = None while True: line = f.readline().rstrip('\r\n') if not line: # Empty line, end of HTTP request. break if line1 is None: line1 = line # Parse HTTP request. # Please note that an assertion here doesn't abort the server. items = line1.split(' ') assert 3 == len(items) assert items[2] in ('HTTP/1.0', 'HTTP/1.1') assert items[0] == 'GET' assert items[1].startswith('/') try: num = int(items[1][1:]) except ValueError: num = None # Write HTTP response. if num is None: f.write('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') f.write('<a href="/0">start at 0</a><p>Hello, World!\n') else: next_num = lprng.Lprng(num).next() f.write('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') f.write('<a href="/%d">continue with %d</a>\n' % (next_num, next_num))
def WsgiApplication(env, start_response): # No need to log the connection, eventlet.wsgi does that. start_response("200 OK", [('Content-Type', 'text/html')]) if env['PATH_INFO'] in ('', '/'): return ['<a href="/0">start at 0</a><p>Hello, World!\n'] else: num = int(env['PATH_INFO'][1:]) next_num = lprng.Lprng(num).next() return ['<a href="/%d">continue with %d</a>\n' % (next_num, next_num)]
def WsgiApplication(env, start_response): print >> sys.stderr, 'connection from %(REMOTE_ADDR)s:%(REMOTE_PORT)s' % env start_response("200 OK", [('Content-Type', 'text/html')]) if env['PATH_INFO'] in ('', '/'): return ['<a href="/0">start at 0</a><p>Hello, World!\n'] else: num = int(env['PATH_INFO'][1:]) next_num = lprng.Lprng(num).next() return ['<a href="/%d">continue with %d</a>\n' % (next_num, next_num)]
def Handler(cs, csaddr): print >> sys.stderr, 'info: connection from %r' % (cs.getpeername(), ) SetFdBlocking(cs, False) csfd = cs.fileno() # Read HTTP request. request = '' while True: # TODO(pts): Implement (in C) and use line buffering. got = ReadAtMost(csfd, 32768) assert got request += got i = request.find('\n\r\n') if i >= 0: j = request.find('\n\n') if j >= 0 and j < i: i = j + 2 else: i += 3 break else: i = request.find('\n\n') if i >= 0: i + 2 break head = request[:i] body = request[i:] # Parse HTTP request. # Please note that an assertion here aborts the server. i = head.find('\n') assert i > 0, (head, ) if head[i - 1] == '\r': line1 = head[:i - 1] else: line1 = head[:i] items = line1.split(' ') assert 3 == len(items) assert items[2] in ('HTTP/1.0', 'HTTP/1.1') assert items[0] == 'GET' assert items[1].startswith('/') try: num = int(items[1][1:]) except ValueError: num = None # Write HTTP response. if num is None: response = ('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n' '<a href="/0">start at 0</a><p>Hello, World!\n') else: next_num = lprng.Lprng(num).next() response = ('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n' '<a href="/%d">continue with %d</a>\n' % (next_num, next_num)) Write(csfd, response) cs.close() # No need for event_del, nothing listening (?).
def WsgiApplication(env, start_response): # Concurrence WSGIServer SUXX: no env['REMOTE_ADDR'] or env['REMOTE_HOST'] start_response("200 OK", [('Content-Type', 'text/html')]) if env['PATH_INFO'] in ('', '/'): return ['<a href="/0">start at 0</a><p>Hello, World!\n'] else: num = int(env['PATH_INFO'][1:]) next_num = lprng.Lprng(num).next() return ['<a href="/%d">continue with %d</a>\n' % (next_num, next_num)]
def handler(client_socket): print >> sys.stderr, 'info: connection from %r' % ( client_socket.socket.getpeername(), ) stream = BufferedStream(client_socket) reader = stream.reader # Strips \r\n and \n from the end. writer = stream.writer # Read HTTP request. line1 = None try: while True: line = reader.read_line() if not line: # Empty line, end of HTTP request. break if line1 is None: line1 = line except EOFError: pass # Parse HTTP request. # Please note that an assertion here doesn't abort the server. items = line1.split(' ') assert 3 == len(items) assert items[2] in ('HTTP/1.0', 'HTTP/1.1') assert items[0] == 'GET' assert items[1].startswith('/') try: num = int(items[1][1:]) except ValueError: num = None # Write HTTP response. if num is None: writer.write_bytes( 'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') writer.write_bytes('<a href="/0">start at 0</a><p>Hello, World!\n') else: next_num = lprng.Lprng(num).next() writer.write_bytes( 'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') writer.write_bytes('<a href="/%d">continue with %d</a>\n' % (next_num, next_num)) writer.flush() stream.close()
def Handler(cs, csaddr): print >>sys.stderr, 'info: connection from %r' % ( cs.getpeername(),) f = cs.makefile('r+') # Read HTTP request. line1 = None while True: line = f.readline().rstrip('\r\n') if not line: # Empty line, end of HTTP request. break if line1 is None: line1 = line # Parse HTTP request. # Please note that an assertion here doesn't abort the server. items = line1.split(' ') assert 3 == len(items) assert items[2] in ('HTTP/1.0', 'HTTP/1.1') assert items[0] == 'GET' assert items[1].startswith('/') try: num = int(items[1][1:]) except ValueError: num = None if num is None: f.write('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') f.write('<a href="/0">start at 0</a><p>Hello, World!\n') else: next_num = lprng.Lprng(num).next() f.write('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') f.write('<a href="/%d">continue with %d</a>\n' % (next_num, next_num)) f.flush() cs.close() # No need for event_del, nothing listening.
def get(self): num = int(self.request.uri[1:]) next_num = lprng.Lprng(num).next() self.write('<a href="/%d">continue with %d</a>\n' % (next_num, next_num))