Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[efe1d1]: examples / web_framework / web_framework.tst Maximize Restore History

Download this file

web_framework.tst    181 lines (166 with data), 4.9 kB

  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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# web_framework.tst
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(br'''
... import sys
... sys.path.insert(0, %r)
... from examples.web_framework import simple_server
... simple_server.run()
... ''' % sys.path[0])
103
>>> 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 [examples.'):
... 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"))
<html>
<head>
<title>It's a Mad, Mad, Mad, Mad World</title>
</head>
<body>
<h1>It's a Mad, Mad, Mad, Mad World</h1>
<h2>Year</h2>
<p>1963</p>
<h2>Length</h2>
<p>3:12</p>
<h2>Directors</h2>
<ol>
<li>Stanley Kramer
</li>
</ol>
</body>
</html>
<BLANKLINE>
>>> 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"))
<html>
<head>
<title>A Funny Thing Happened on the Way to the Forum</title>
</head>
<body>
<h1>A Funny Thing Happened on the Way to the Forum</h1>
<h2>Year</h2>
<p>1966</p>
<h2>Length</h2>
<p>1:39</p>
<h2>Directors</h2>
<ol>
<li>Richard Lester
</li>
</ol>
</body>
</html>
<BLANKLINE>
>>> readline() # doctest: +ELLIPSIS
'localhost - - [...] "GET /movie/3/movie.html HTTP/1.1" 200 332\n'
>>> print(get("movie/3/movie2.html"))
<html>
<head>
<title>A Funny Thing Happened on the Way to the Forum</title>
</head>
<body>
<h1>A Funny Thing Happened on the Way to the Forum</h1>
<h2>Year:</h2>
<p>1966</p>
<h2>Length:</h2>
<p>1:39</p>
<h2>Directors:</h2>
<ol>
<li>Richard Lester
</li>
</ol>
<h2>DVD List:</h2>
<ol>
<li>102(1)
</li>
<li>105(1)
</li>
</ol>
</body>
</html>
<BLANKLINE>
>>> 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"))
<html>
<head>
<title>Casino Royale</title>
</head>
<body>
<h1>Casino Royale</h1>
<h2>Year:</h2>
<p>1967</p>
<h2>Length:</h2>
<p>2:11</p>
<h2>Directors:</h2>
<ol>
<li>Val Guest
</li>
<li>Ken Hughes
</li>
<li>John Huston
</li>
<li>Joseph McGrath
</li>
<li>Robert Parrish
</li>
<li>Richard Talmadge
</li>
</ol>
<h2>DVD List:</h2>
<ol>
<li>104(1)
</li>
</ol>
</body>
</html>
<BLANKLINE>
>>> 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