# web_framework.tst >>> import sys >>> import pyke >>> import os >>> source_dir = os.path.dirname(os.path.dirname(pyke.__file__)) >>> new_path = os.path.join(source_dir, 'examples') >>> sys.path.append(new_path) First, fire up the server: >>> import sys >>> import os >>> import signal >>> import time >>> import subprocess >>> server = subprocess.Popen((sys.executable, "-u", "-"), ... stdin=subprocess.PIPE, ... stdout=subprocess.PIPE, ... stderr=subprocess.STDOUT, ... close_fds=True, env=os.environ) >>> server.stdin.write((r''' ... import sys ... sys.path.append(%r) ... sys.path.append(%r) ... from web_framework import simple_server ... simple_server.run() ... ''' % (new_path, source_dir)).encode()) > 110 True >>> server.stdin.close() >>> def readline(): ... global server_error_msg ... if server.poll() is not None: ... msg = server.stdout.read() ... if msg: ... server_error_msg = '\n'.join(('server: ' + line) ... for line in msg.split('\n')) ... sys.stdout.write(server_error_msg) ... sys.stdout.write( ... "Server terminated prematurely with returncode %r\n" % \ ... (server.returncode,)) ... return ... line = server.stdout.readline().decode() ... print(line, end='', file=sys.stderr) ... return line >>> while True: ... line = readline() ... if not line.startswith('writing ['): ... break >>> line 'Server running...\n' >>> time.sleep(0.8) Then interact with it: >>> import urllib.request >>> def get(path): ... f = urllib.request.urlopen("http://localhost:8080/" + path) ... #print("info:", tuple(sorted(f.info().keys())), file=sys.stderr) ... try: ... return f.read().decode() # int(f.info()['content-length'])) ... finally: ... f.close() >>> print(get("movie/1/movie.html")) It's a Mad, Mad, Mad, Mad World

It's a Mad, Mad, Mad, Mad World

Year

1963

Length

3:12

Directors

  1. Stanley Kramer
>>> readline() # doctest: +ELLIPSIS "get_plan(..., ('movie',), ...examples/web_framework/movie.html)\n" >>> readline() # doctest: +ELLIPSIS 'localhost - - [...] "GET /movie/1/movie.html HTTP/1.1" 200 302\n' >>> print(get("movie/3/movie.html")) A Funny Thing Happened on the Way to the Forum

A Funny Thing Happened on the Way to the Forum

Year

1966

Length

1:39

Directors

  1. Richard Lester
>>> readline() # doctest: +ELLIPSIS 'localhost - - [...] "GET /movie/3/movie.html HTTP/1.1" 200 332\n' >>> print(get("movie/3/movie2.html")) A Funny Thing Happened on the Way to the Forum

A Funny Thing Happened on the Way to the Forum

Year:

1966

Length:

1:39

Directors:

  1. Richard Lester

DVD List:

  1. 102(1)
  2. 105(1)
>>> readline() # doctest: +ELLIPSIS "get_plan(..., ('movie',), ...examples/web_framework/movie2.html)\n" >>> readline() # doctest: +ELLIPSIS 'localhost - - [...] "GET /movie/3/movie2.html HTTP/1.1" 200 429\n' >>> print(get("movie/6/movie2.html")) Casino Royale

Casino Royale

Year:

1967

Length:

2:11

Directors:

  1. Val Guest
  2. Ken Hughes
  3. John Huston
  4. Joseph McGrath
  5. Robert Parrish
  6. Richard Talmadge

DVD List:

  1. 104(1)
>>> readline() # doctest: +ELLIPSIS 'localhost - - [...] "GET /movie/6/movie2.html HTTP/1.1" 200 485\n' Kill server: >>> time.sleep(0.4) >>> os.kill(server.pid, signal.SIGTERM) >>> server.wait() -15