Skip to content

Commit 396ac9f

Browse files
author
Casey Boettcher
committed
Multiple IPs working from webui
1 parent e3c26b6 commit 396ac9f

4 files changed

Lines changed: 72 additions & 32 deletions

File tree

README.md

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
### Python Challenge
22

33
```
4-
Usage: xfipchk.py [-h] api_authN_file [ip_address]
4+
usage: xfipchk.py [-h] {cli,web} ...
5+
6+
Use the X-Force API to check IP address reputation.
57
68
positional arguments:
7-
api_authN_file Path to a file containing your X-Force credentials, key and
8-
password on first and second lines, respectively.
9-
10-
ip_address An IP address to be checked via X-Force. If the IP address
11-
is omitted or invalid, the user will be prompted for one.
12-
13-
optional arguments:
14-
-h, --help show this help message and exit
9+
{cli,web} Mutually exclusive sub-commands
10+
cli Command-line Interface; run 'xfipchk cli -h' to see options
11+
web Web interface; run 'xfipchk web -h' to see options
12+
13+
14+
cli:
15+
xfipchk.py cli [-h] [-o [output_file]]
16+
[-i ip_address | -I file_of_ip_addresses]
17+
authN
18+
19+
positional arguments:
20+
authN Path to a file containing your X-Force credentials,
21+
key and password on first and second lines,
22+
respectively.
23+
24+
optional arguments:
25+
-h, --help show this help message and exit
26+
-o [output_file], --out [output_file]
27+
Write result of X-Force call to file; if this option
28+
is elected but no filename is provided, a file will be
29+
created for the user.
30+
-i ip_address, --ip ip_address
31+
An IP address to be checked via X-Force. If the
32+
IPaddress is omitted or invalid, the user will be
33+
prompted for one.
34+
-I file_of_ip_addresses, --Ips file_of_ip_addresses
35+
A file containing IP addresses, one per line.
36+
37+
web:
38+
usage: xfipchk.py web [-h] [-p PORT] [-a ADDRESS]
39+
40+
Web Interface:
41+
You may specify the address and port to bind to; defaults are 127.0.0.1
42+
and 8000
43+
44+
-p PORT, --port PORT
45+
-a ADDRESS, --address ADDRESS
46+
47+
1548
```

web/webui.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def process_form(self, api_key, api_password, ip_addresses):
2121
if xfipchk.validate_api_creds(api_key.strip()) and xfipchk.validate_api_creds(api_password.strip()):
2222
form_ips = []
2323
if isinstance(ip_addresses, str):
24-
form_ips.append(ip_addresses)
25-
elif isinstance(ip_addresses, list):
26-
form_ips.extend(ip_addresses)
24+
form_ips = ip_addresses.splitlines()
2725
good_ips = []
2826
for i in form_ips:
2927
if xfipchk.validate_ip(i):
@@ -33,8 +31,26 @@ def process_form(self, api_key, api_password, ip_addresses):
3331
return cherrypy.HTTPError("400: Bad Request", 400)
3432

3533
@cherrypy.expose()
36-
def stop_demo(self, stop_demo):
34+
def stop_demo(self):
3735
cherrypy.engine.stop()
3836

37+
3938
if __name__ == '__main__':
40-
xfipchk.start_server()
39+
webapp = XforceForm('127.0.0.1', 8000)
40+
#d = cherrypy.process.plugins.Daemonizer(cherrypy.engine)
41+
#d.subscribe()
42+
cherrypy.tree.mount(webapp, config='./server.cfg')
43+
44+
if hasattr(cherrypy.engine, 'signal_handler'):
45+
cherrypy.engine.signal_handler.subscribe()
46+
47+
# Initialize console control
48+
if hasattr(cherrypy.engine, "console_control_handler"):
49+
cherrypy.engine.console_control_handler.subscribe()
50+
51+
cherrypy.engine.start()
52+
53+
#pidfile = tempfile.TemporaryFile(prefix='xfipchk', suffix='.pid')
54+
#PIDFile(cherrypy.engine, pidfile).subscribe()
55+
56+
cherrypy.engine.block()

web/xfipchk.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414

1515
if (conf)
1616
{
17-
$.post( "/stop_demo" )
17+
$.ajax( {url: "/stop_demo"} );
1818
}
1919
}
2020

2121
function check_ips()
2222
{
2323
var req = $.ajax({'url': '/process_form'});
24-
req.done(function (data){ $( ".result" ).html( data ); }, "json");
24+
req.done(function (data){
25+
$.getElementById("results_div").setAttribute("hidden")
26+
$( ".result" ).html( data ); }, "json");
2527
}
2628

2729
</script>

xfipchk.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
XFORCE_API_IP_REP = 'ipr'
2525
XFORCE_CRED_PATTERN = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
2626

27-
global pidfile
28-
2927

3028
def parse_args():
3129
"""
@@ -152,7 +150,9 @@ def call_xforce_api(address_list, key, password):
152150
Call the ipr method of the X-Force API using the IP address(es) contained in the parameter. Results are written
153151
to a file or stdout (default).
154152
155-
:param address_list: a list of IP addresses
153+
:param address_list: a list of IP addresses
154+
:param key: X-Force API key
155+
:param password: X-Force API password
156156
:return: a list of json objects
157157
"""
158158
results = []
@@ -195,33 +195,22 @@ def start_server(address='127.0.0.1', port=8000):
195195
webapp = web.webui.XforceForm(address, port)
196196
d = cherrypy.process.plugins.Daemonizer(cherrypy.engine)
197197
d.subscribe()
198-
#cherrypy.config.update('server.cfg')
199198
cherrypy.tree.mount(webapp, config='./server.cfg')
200-
#cherrypy.tree.mount(webapp)
201199
cherrypy.engine.start()
200+
202201
pidfile = tempfile.TemporaryFile(prefix='xfipchk', suffix='.pid')
203202
PIDFile(cherrypy.engine, pidfile).subscribe()
204203

205-
cherrypy.engine.subscribe('stop', cleanup(pidfile))
206204
cherrypy.engine.block()
207205

208-
return pidfile.name
209-
210-
211-
def cleanup(pid_file):
212-
print(pid_file.name)
213-
if os.path.exists(pid_file):
214-
cherrypy.engine.stop()
215-
216206

217207
def main():
218-
global pidfile
219208
args = parse_args()
220209
# if port is in Namespace object, assume web interface
221210
if hasattr(args, 'port'):
222211
# TODO: should use a context manager here
223212
try:
224-
pidfile = start_server(args.address, args.port)
213+
start_server(args.address, args.port)
225214
except (ConnectionError, KeyboardInterrupt) as err:
226215
print("Server failed to start: {}".format(err))
227216

0 commit comments

Comments
 (0)