-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-server.py
executable file
·244 lines (224 loc) · 11 KB
/
time-server.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
'''
This program is free software; you can redistribute it and/or modify
it under the terms of the Revised BSD License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Revised BSD License for more details.
Copyright 2016 Game Maker 2k - https://github.com/GameMaker2k
Copyright 2016 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
$FileInfo: time-server.py - Last Update: 3/16/2016 Ver. 0.0.1 RC 1 - Author: cooldude2k $
'''
import cherrypy
import argparse
import time
import datetime
parser = argparse.ArgumentParser(description="Test Time Server.")
parser.add_argument("--ver", "--version", action="version",
version="Test Time Server 0.0.1")
parser.add_argument("--port", "--port-number", default=4123,
help="port number to use for server.")
parser.add_argument("--host", "--host-name",
default="127.0.0.1", help="host name to use for server.")
parser.add_argument("--verbose", "--verbose-mode",
help="show log on terminal screen.", action="store_true")
parser.add_argument("--gzip", "--gzip-mode",
help="enable gzip http requests.", action="store_true")
parser.add_argument("--gzipfilter", "--gzipfilter-mode",
help="enable gzipfilter mode.", action="store_true")
parser.add_argument("--accesslog", "--accesslog-file",
help="location to store access log file.")
parser.add_argument("--errorlog", "--errorlog-file",
help="location to store error log file.")
parser.add_argument("--timeout", "--response-timeout", default=6000,
help="the number of seconds to allow responses to run.")
parser.add_argument("--environment", "--server-environment", default="production",
help="The server.environment entry controls how CherryPy should run.")
getargs = parser.parse_args()
if(getargs.port is not None):
port = int(getargs.port)
else:
port = 4123
if(getargs.host is not None):
host = str(getargs.host)
else:
host = "127.0.0.1"
if(getargs.timeout is not None):
timeout = int(getargs.timeout)
else:
timeout = 6000
if(getargs.accesslog is not None):
accesslog = str(getargs.accesslog)
else:
accesslog = "./access.log"
if(getargs.errorlog is not None):
errorlog = str(getargs.errorlog)
else:
errorlog = "./errors.log"
if(getargs.environment is not None):
serv_environ = str(getargs.environment)
else:
serv_environ = "production"
pro_app_name = "Test Time Server"
pro_app_version = "0.0.1"
pro_app_subname = " "+pro_app_version
class GenerateIndexPage(object):
def index(self):
now = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
ts = now.strftime("%s")
utc_ts = utc_now.strftime("%s")
ts_str = str(ts)
utc_ts_str = str(utc_ts)
ts_ms = now.microsecond
utc_ts_ms = utc_now.microsecond
ts_ms_str = str(ts_ms).ljust(6, "0")
utc_ts_ms_str = str(utc_ts_ms).ljust(6, "0")
ts_full_str = ts_str+"."+ts_ms_str
utc_ts_full_str = utc_ts_str+"."+utc_ts_ms_str
cherrypy.response.headers['Content-Type'] = 'text/html; charset=UTF-8'
cherrypy.response.headers['Datetime-Timestamp'] = utc_ts_str
cherrypy.response.headers['Datetime-Microseconds'] = utc_ts_ms_str
cherrypy.response.headers['Datetime-Timestampfull'] = utc_ts_full_str
return "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"utf-8\">\n <title>"+pro_app_name+pro_app_subname+"</title>\n </head>\n <body>\n <h1>"+pro_app_name+pro_app_subname+"</h1>\n <h6>\n Timestamp: "+utc_ts_str+"<br />\n Microseconds: "+utc_ts_ms_str+"<br />\n Timestampfull: "+utc_ts_full_str+"<br />\n </h6>\n <p>\n <a href=\"/csv/\">CSV</a>\n <a href=\"/txt/\">TXT</a>\n <a href=\"/xml/\">XML</a>\n <a href=\"/json/\">JSON</a>\n <a href=\"/text/\">TEXT</a>\n <a href=\"/yaml/\">YAML</a>\n </p>\n </body>\n</html>\n"
index.exposed = True
class GenerateCSVPage(object):
def index(self):
now = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
ts = now.strftime("%s")
utc_ts = utc_now.strftime("%s")
ts_str = str(ts)
utc_ts_str = str(utc_ts)
ts_ms = now.microsecond
utc_ts_ms = utc_now.microsecond
ts_ms_str = str(ts_ms).ljust(6, "0")
utc_ts_ms_str = str(utc_ts_ms).ljust(6, "0")
ts_full_str = ts_str+"."+ts_ms_str
utc_ts_full_str = utc_ts_str+"."+utc_ts_ms_str
cherrypy.response.headers['Content-Type'] = 'application/json; charset=UTF-8'
cherrypy.response.headers['Datetime-Timestamp'] = utc_ts_str
cherrypy.response.headers['Datetime-Microseconds'] = utc_ts_ms_str
cherrypy.response.headers['Datetime-Timestampfull'] = utc_ts_full_str
return "timestamp,microseconds,timestampfull\n"+utc_ts_str+","+utc_ts_ms_str+","+utc_ts_full_str+"\n"
index.exposed = True
class GenerateTXTPage(object):
def index(self):
now = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
ts = now.strftime("%s")
utc_ts = utc_now.strftime("%s")
ts_str = str(ts)
utc_ts_str = str(utc_ts)
ts_ms = now.microsecond
utc_ts_ms = utc_now.microsecond
ts_ms_str = str(ts_ms).ljust(6, "0")
utc_ts_ms_str = str(utc_ts_ms).ljust(6, "0")
ts_full_str = ts_str+"."+ts_ms_str
utc_ts_full_str = utc_ts_str+"."+utc_ts_ms_str
cherrypy.response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
cherrypy.response.headers['Datetime-Timestamp'] = utc_ts_str
cherrypy.response.headers['Datetime-Microseconds'] = utc_ts_ms_str
cherrypy.response.headers['Datetime-Timestampfull'] = utc_ts_full_str
return ""+utc_ts_str+"\n"+utc_ts_ms_str+"\n"+utc_ts_full_str+"\n"
index.exposed = True
class GenerateXMLPage(object):
def index(self):
now = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
ts = now.strftime("%s")
utc_ts = utc_now.strftime("%s")
ts_str = str(ts)
utc_ts_str = str(utc_ts)
ts_ms = now.microsecond
utc_ts_ms = utc_now.microsecond
ts_ms_str = str(ts_ms).ljust(6, "0")
utc_ts_ms_str = str(utc_ts_ms).ljust(6, "0")
ts_full_str = ts_str+"."+ts_ms_str
utc_ts_full_str = utc_ts_str+"."+utc_ts_ms_str
cherrypy.response.headers['Content-Type'] = 'application/xml; charset=UTF-8'
cherrypy.response.headers['Datetime-Timestamp'] = utc_ts_str
cherrypy.response.headers['Datetime-Microseconds'] = utc_ts_ms_str
cherrypy.response.headers['Datetime-Timestampfull'] = utc_ts_full_str
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<datetime>\n <time timestamp=\""+utc_ts_str+"\" microseconds=\""+utc_ts_ms_str+"\" timestampfull=\""+utc_ts_full_str+"\" />\n</datetime>\n"
index.exposed = True
class GenerateJSONPage(object):
def index(self):
now = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
ts = now.strftime("%s")
utc_ts = utc_now.strftime("%s")
ts_str = str(ts)
utc_ts_str = str(utc_ts)
ts_ms = now.microsecond
utc_ts_ms = utc_now.microsecond
ts_ms_str = str(ts_ms).ljust(6, "0")
utc_ts_ms_str = str(utc_ts_ms).ljust(6, "0")
ts_full_str = ts_str+"."+ts_ms_str
utc_ts_full_str = utc_ts_str+"."+utc_ts_ms_str
cherrypy.response.headers['Content-Type'] = 'text/csv; charset=UTF-8'
cherrypy.response.headers['Datetime-Timestamp'] = utc_ts_str
cherrypy.response.headers['Datetime-Microseconds'] = utc_ts_ms_str
cherrypy.response.headers['Datetime-Timestampfull'] = utc_ts_full_str
return "{\n \"datetime\": {\n \"time\": {\n \"-timestamp\": \""+utc_ts_str+"\",\n \"-microseconds\": \""+utc_ts_ms_str+"\",\n \"-timestampfull\": \""+utc_ts_full_str+"\"\n }\n }\n}\n"
index.exposed = True
class GenerateTEXTPage(object):
def index(self):
now = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
ts = now.strftime("%s")
utc_ts = utc_now.strftime("%s")
ts_str = str(ts)
utc_ts_str = str(utc_ts)
ts_ms = now.microsecond
utc_ts_ms = utc_now.microsecond
ts_ms_str = str(ts_ms).ljust(6, "0")
utc_ts_ms_str = str(utc_ts_ms).ljust(6, "0")
ts_full_str = ts_str+"."+ts_ms_str
utc_ts_full_str = utc_ts_str+"."+utc_ts_ms_str
cherrypy.response.headers['Content-Type'] = 'text/plain; charset=UTF-8'
cherrypy.response.headers['Datetime-Timestamp'] = utc_ts_str
cherrypy.response.headers['Datetime-Microseconds'] = utc_ts_ms_str
cherrypy.response.headers['Datetime-Timestampfull'] = utc_ts_full_str
return ""+utc_ts_str+"\n"+utc_ts_ms_str+"\n"+utc_ts_full_str+"\n"
index.exposed = True
class GenerateYAMLPage(object):
def index(self):
now = datetime.datetime.now()
utc_now = datetime.datetime.utcnow()
ts = now.strftime("%s")
utc_ts = utc_now.strftime("%s")
ts_str = str(ts)
utc_ts_str = str(utc_ts)
ts_ms = now.microsecond
utc_ts_ms = utc_now.microsecond
ts_ms_str = str(ts_ms).ljust(6, "0")
utc_ts_ms_str = str(utc_ts_ms).ljust(6, "0")
ts_full_str = ts_str+"."+ts_ms_str
utc_ts_full_str = utc_ts_str+"."+utc_ts_ms_str
cherrypy.response.headers['Content-Type'] = 'application/json; charset=UTF-8'
cherrypy.response.headers['Datetime-Timestamp'] = utc_ts_str
cherrypy.response.headers['Datetime-Microseconds'] = utc_ts_ms_str
cherrypy.response.headers['Datetime-Timestampfull'] = utc_ts_full_str
return "---\n datetime: \n time: \n -timestamp: \""+utc_ts_str+"\"\n -microseconds: \""+utc_ts_ms_str+"\"\n -timestampfull: \""+utc_ts_full_str+"\"\n"
index.exposed = True
cherrypy.config.update({"environment": serv_environ,
"log.error_file": errorlog,
"log.access_file": accesslog,
"log.screen": getargs.verbose,
"gzipfilter.on": getargs.gzipfilter,
"tools.gzip.on": getargs.gzip,
"tools.gzip.mime_types": ['text/*'],
"server.socket_host": host,
"server.socket_port": port,
"response.timeout": timeout,
})
cherrypy.root = GenerateIndexPage()
cherrypy.root.csv = GenerateCSVPage()
cherrypy.root.txt = GenerateTXTPage()
cherrypy.root.xml = GenerateXMLPage()
cherrypy.root.json = GenerateJSONPage()
cherrypy.root.text = GenerateTEXTPage()
cherrypy.root.yaml = GenerateYAMLPage()
cherrypy.quickstart(cherrypy.root)