-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
executable file
·65 lines (47 loc) · 1.79 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
#############################################
# @title What Is My IP (Python version)
#
# @author Pierre-Henry Soria <pierrehenrysoria@gmail.com>
# @copyright Pierre-Henry Soria, All Rights Reserved.
# @license Lesser General Public License (LGPL) (http://www.gnu.org/copyleft/lesser.html)
# @version 1.0
#
#############################################
from cgi import escape
from os import environ
from ConfigParser import SafeConfigParser
INDEX_FILE = "index.tpl"
# Set the HTML type to HTTP header
print("Content-type: text/html")
print("")
# Get the template file
def tpl_file(tpl_file):
try:
file = open('template/' + tpl_file, 'r')
return file.read()
except IOError:
print("Can't open the tpl file: ", tpl_file)
finally:
file.close()
# Get configuration file
try:
config = SafeConfigParser()
config.read("config/conf.ini")
except IOError as e_msg:
print("Can't open the config file: ", e_msg)
# Template Parsing
html = tpl_file(INDEX_FILE)
html = html.replace("{include_header}", tpl_file('header.inc.tpl'))
html = html.replace("{include_footer}", tpl_file('footer.inc.tpl'))
html = html.replace("{include_analytics}", tpl_file('analytics.inc.tpl'))
html = html.replace("{url}", config.get("webapp", "url"))
html = html.replace("{style_name}", config.get("webapp", "style_name"))
html = html.replace("{site_name}", config.get("general", "site_name"))
html = html.replace("{contact_email}", config.get("general", "contact_email"))
html = html.replace("{api_ip_url}", config.get("api", "ip_url"))
html = html.replace("{analytics_id}", config.get("api", "analytics_id"))
html = html.replace("{contact_email}", config.get("general", "contact_email"))
html = html.replace("{ip}", escape(environ["REMOTE_ADDR"]))
# Output
print(html)