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

Download this file

177 lines (162 with data), 4.5 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
# web_framework.tst
First, fire up the server:
>>> import sys
>>> import os
>>> import signal
>>> import time
>>> import subprocess
>>> server = subprocess.Popen(("python2.5", "-i"),
... stdin=subprocess.PIPE,
... stdout=subprocess.PIPE,
... stderr=subprocess.PIPE,
... close_fds=True, env=os.environ)
>>> server.stderr.readline() # doctest: +ELLIPSIS
'Python ...\n'
>>> server.stderr.readline() # doctest: +ELLIPSIS
'[...\n'
>>> server.stderr.readline() # doctest: +ELLIPSIS
'Type ...\n'
>>> server.stderr.read(4)
'>>> '
>>> server.stdin.write("print 'mom'\n")
>>> server.stdout.readline()
'mom\n'
>>> server.stderr.read(4)
'>>> '
>>> server.stdin.write("import sys\n")
>>> server.stderr.read(4)
'>>> '
>>> server.stdin.write("sys.path.insert(0, '')\n")
>>> server.stderr.read(4)
'>>> '
>>> server.stdin.write("import simple_server\n")
>>> server.stderr.read(4)
'>>> '
>>> server.stdin.write("print 'dad'\n")
>>> server.stdout.readline()
'dad\n'
>>> server.stderr.read(4)
'>>> '
>>> server.stdin.write("simple_server.run()\n")
>>> time.sleep(0.4)
Then interact with it:
>>> import urllib
>>> def get(path):
... f = urllib.urlopen("http://localhost:8080/" + path)
... #print >> sys.stderr, "info:", tuple(sorted(f.info().keys()))
... try:
... return f.read() # 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:00</p>
<h2>Directors</h2>
<ol>
<li>Stanley Kramer
</li>
</ol>
</body>
</html>
<BLANKLINE>
#>>> server.stderr.readline() # doctest: +ELLIPSIS
#'localhost - - [...] "GET /movie/1/movie.html HTTP/1.0" 200 305\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:00</p>
<h2>Directors</h2>
<ol>
<li>Richard Lester
</li>
</ol>
</body>
</html>
<BLANKLINE>
#>>> server.stderr.readline() # doctest: +ELLIPSIS
#'localhost - - [...] "GET /movie/3/movie.html HTTP/1.0" 200 335\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:00</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>
#>>> server.stderr.readline() # doctest: +ELLIPSIS
#'localhost - - [...] "GET /movie/3/movie2.html HTTP/1.0" 200 432\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:00</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>
#>>> server.stderr.readline() # doctest: +ELLIPSIS
#'localhost - - [...] "GET /movie/6/movie2.html HTTP/1.0" 200 488\n'
Kill server:
>>> time.sleep(0.4)
>>> os.kill(server.pid, signal.SIGINT)
>>> server.stdin.close()
>>> server.wait()
0

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks