forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypermoon.py
executable file
·51 lines (37 loc) · 1.07 KB
/
hypermoon.py
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
#!/usr/bin/env python3
# Invent silly names for the moon
import sys, os
import random
import cgi
def hypermoon(filename, num=4):
with open(filename, encoding='utf-8') as fp:
lines = fp.readlines()
words = [ lines[random.randint(0, len(lines))].strip()
for i in range(num) ]
words.append('moon')
return ' '.join(words)
if __name__ == '__main__':
random.seed()
num = 4
if 'REQUEST_METHOD' in os.environ:
print('''Content-Type: text/html
<head>
<title>Tonight's Moon</title>
</head>
<body>''')
form = cgi.FieldStorage()
if 'nwords' in form:
try:
num = int(form['nwords'].value)
except:
print("<p>I don't understand 'nwords=%s'"
% form['nwords'].value)
num = 4
print("<p>Tonight's moon is a <b>")
else:
form = None
if len(sys.argv) > 1:
num = int(sys.argv[1])
print(hypermoon('/usr/share/dict/words', num))
if form:
print('</b>\n</body>\n</html>')