Skip to content

Commit bf72ef2

Browse files
author
Casey Boettcher
committed
switched to cherrpy daemon
1 parent 9a07da9 commit bf72ef2

File tree

9 files changed

+64
-15
lines changed

9 files changed

+64
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ out
55
gen
66
test/xforce.cfg
77
notes.txt
8-
8+
keys.txt
9+
__pycache__/*

__init__.py

Whitespace-only changes.

test/__init__.py

Whitespace-only changes.

test/data/creds.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
720e6119-4e9f-4488-bd9c-9b270f18aebe
2+
5338fcd2-28b1-4c09-a698-607$11bd54b0
3+
5339fcd2-28c1-4d09-a798-617911bd54b0
4+
5338fcd2-28b1-4c09-a698-607911d54b0
5+
5338fcd2-28b1-4c09-a698-627911bd54b0
6+
338fcd2-28b1-4c09-a698-607911bd54b0
7+
5338fcd2-28b1-4c09-a698-607911bd54b0
8+
5z38fcd2-28b1-4c09-a6x8-607911bdy4b0
9+
5338fcd2-28b1-4c09-a698-607911bd54b0
10+
338fcd22-28b1-4c09-A698-607911bd54b0
11+
5338-28b1-adc84c09-a695208-6079154b0

test/ips.txt renamed to test/data/ips.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ cc85:e78:948c:2ad6:5959:102d:10cb:31c4
1515
dbb1:39b7:1e8f:1a2a:3737:9721:5d16:166
1616
131.isle.12.9
1717
1d67:bd71:56d9:13f3:5499:25b:cc84:f7e4
18-
f4bc:bab1:4d39:9c0b:8797:a867:e6ce:6629
18+
xbab1:4M39:9c0b:87j7:a867:e6ce:6629
1919
193.131.176.37
2020
239.244.72.99

test/test_xfipchk.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
1+
import os
2+
import requests
13
import unittest
24
import xfipchk
35

46

57
class TestXfipchk(unittest.TestCase):
6-
API_CREDS = './xforce.cfg'
8+
API_CREDS = '../keys.txt'
9+
CREDS_TEST_FILE = './data/creds.txt'
10+
ADDRESS_TEST_FILE = './data/ips.txt'
711

812
def test_validate_ip(self):
9-
ADDRESS_FILE = './ips.txt'
1013
BAD_IPS = 4
1114
GOOD_IPS = 16
12-
13-
good, bad = 0
14-
with open(ADDRESS_FILE, 'r') as ipfile:
15+
good = bad = 0
16+
with open(self.ADDRESS_TEST_FILE, 'r') as ipfile:
1517
for ip in ipfile:
16-
if xfipchk(ip):
18+
if xfipchk.validate_ip(ip):
1719
good += 1
1820
else:
1921
bad += 1
2022

2123
assert (good == GOOD_IPS and bad == BAD_IPS)
2224

25+
def test_validate_api_creds(self):
26+
good = bad = 0
27+
GOOD_TOKENS = 5
28+
BAD_TOKENS = 6
29+
with open(self.CREDS_TEST_FILE, 'r') as file:
30+
for line in file:
31+
if xfipchk.validate_api_creds(line.strip()):
32+
good += 1
33+
else:
34+
bad += 1
35+
36+
def test_start_server(self):
37+
os.chdir('..')
38+
xfipchk.start_server()
39+
try:
40+
r = requests.get('http://127.0.0.1:8080')
41+
except:
42+
assert False
43+
2344

2445
if __name__ == '__main__':
2546
unittest.main()

web/__init__.py

Whitespace-only changes.

web/server.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ tools.sessions.on: True
44
tools.staticdir.on = True
55
tools.staticdir.root = os.path.abspath('.')
66
tools.staticdir.dir = os.path.join(os.path.abspath('.'), 'web')
7-
tools.staticdir.index = "xfipchk.html"

xfipchk.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import IPy
1919
import web.webui
2020
import cherrypy
21+
from cherrypy.process.plugins import PIDFile
2122

2223
XFORCE_API_BASE = 'https://api.xforce.ibmcloud.com'
2324
XFORCE_API_IP_REP = 'ipr'
@@ -187,23 +188,39 @@ def print_json_file(results, file):
187188

188189

189190
def start_server(address='127.0.0.1', port=8000):
191+
os.chdir('./web')
192+
print("This is getcwd 1: {}".format(os.path.abspath(os.getcwd())))
193+
print("This is getcwd 2: {}".format(os.getcwd()))
194+
config = {'/':
195+
{
196+
'tools.staticdir.on': True,
197+
'tools.staticdir.root': os.path.abspath(os.getcwd()),
198+
'tools.staticdir.index': "xfipchk.html",
199+
'tools.staticdir.dir': os.getcwd()
200+
}
201+
}
190202
webapp = web.webui.XforceForm(address, port)
191-
cherrypy.quickstart(webapp, '/', './web/server.cfg')
203+
d = cherrypy.process.plugins.Daemonizer(cherrypy.engine)
204+
d.subscribe()
205+
cherrypy.config.update(config)
206+
cherrypy.tree.mount(webapp, config=config)
207+
cherrypy.engine.start()
208+
pidfile = tempfile.TemporaryFile(suffix='.pid')
209+
PIDFile(cherrypy.engine, pidfile).subscribe()
210+
cherrypy.engine.block()
192211

193212

194213
def main():
195214
args = parse_args()
196215
# if port is in Namespace object, assume web interface
197216
if hasattr(args, 'port'):
198217
# TODO: should use a context manager here
199-
current = os.curdir
200218
try:
201-
os.chdir('./web')
202-
start_server(args.address, args.port)
203-
except OSError as ose:
219+
start_server(args.address, args.port, pidfile)
220+
except:
204221
print(ose.strerror)
205222
finally:
206-
os.chdir(current)
223+
207224
# assume cli if user passed in api key file
208225
elif hasattr(args, 'authN'):
209226
ip = None

0 commit comments

Comments
 (0)