本文整理汇总了Python中thor.time函数的典型用法代码示例。如果您正苦于以下问题:Python time函数的具体用法?Python time怎么用?Python time使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了time函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_test
def save_test(self):
"""Save a previously run test_id."""
try:
# touch the save file so it isn't deleted.
os.utime(
os.path.join(save_dir, self.test_id),
(
thor.time(),
thor.time() + (save_days * 24 * 60 * 60)
)
)
location = "?id=%s" % self.test_id
if self.descend:
location = "%s&descend=True" % location
self.response_start(
"303", "See Other", [
("Location", location)
])
self.response_body("Redirecting to the saved test page...")
except (OSError, IOError):
self.response_start(
"500", "Internal Server Error", [
("Content-Type", "text/html; charset=%s" % charset),
])
# TODO: better error message (through formatter?)
self.response_body(
error_template % "Sorry, I couldn't save that."
)
self.response_done([])
示例2: write
def write(self, content: bytes, lifetime: int) -> None:
"""
Write content to the file, marking it fresh for lifetime seconds.
Discard errors silently.
"""
try:
fd = gzip.open(self.path, 'w')
fd.write(content)
os.utime(self.path, (thor.time(), thor.time() + lifetime))
except (OSError, IOError, zlib.error):
return
finally:
if 'fd' in locals():
fd.close()
示例3: _response_done
def _response_done(self, trailers):
"Finish anaylsing the response, handling any parse errors."
self._st.append('_response_done()')
state = self.state
state.res_complete = True
state.res_done_ts = thor.time()
state.transfer_length = self.exchange.input_transfer_length
state.header_length = self.exchange.input_header_length
# TODO: check trailers
if self.status_cb and state.type:
self.status_cb("fetched %s (%s)" % (state.uri, state.type))
state.res_body_md5 = self._md5_processor.digest()
state.res_body_post_md5 = self._md5_post_processor.digest()
checkCaching(state)
if state.method not in ['HEAD'] and state.res_status not in ['304']:
# check payload basics
if state.parsed_hdrs.has_key('content-length'):
if state.res_body_len == state.parsed_hdrs['content-length']:
state.set_message('header-content-length', rs.CL_CORRECT)
else:
state.set_message('header-content-length',
rs.CL_INCORRECT,
body_length=f_num(state.res_body_len)
)
if state.parsed_hdrs.has_key('content-md5'):
c_md5_calc = base64.encodestring(state.res_body_md5)[:-1]
if state.parsed_hdrs['content-md5'] == c_md5_calc:
state.set_message('header-content-md5', rs.CMD5_CORRECT)
else:
state.set_message('header-content-md5', rs.CMD5_INCORRECT,
calc_md5=c_md5_calc)
self.done()
self.finish_task()
示例4: body_done
def body_done(self, complete: bool, trailers: RawHeaderListType = None) -> None:
"""
Signal that the body is done. Complete should be True if we
know it's complete (e.g., final chunk, Content-Length).
"""
self.complete = complete
self.complete_time = thor.time()
self.trailers = trailers or []
self.payload_md5 = self._md5_processor.digest()
self.decoded_md5 = self._md5_post_processor.digest()
if self.is_request or \
(not self.is_head_response and self.status_code not in ['304']):
# check payload basics
if 'content-length' in self.parsed_headers:
if self.payload_len == self.parsed_headers['content-length']:
self.add_note('header-content-length', CL_CORRECT)
else:
self.add_note('header-content-length',
CL_INCORRECT,
body_length=f_num(self.payload_len))
if 'content-md5' in self.parsed_headers:
c_md5_calc = base64.encodebytes(self.payload_md5)[:-1]
if self.parsed_headers['content-md5'] == c_md5_calc:
self.add_note('header-content-md5', CMD5_CORRECT)
else:
self.add_note('header-content-md5',
CMD5_INCORRECT, calc_md5=c_md5_calc)
self.emit('content_available')
示例5: run
def run(self, done_cb=None):
"""
Make an asynchronous HTTP request to uri, calling status_cb as it's
updated and done_cb when it's done. Reason is used to explain what the
request is in the status callback.
"""
self.outstanding_tasks += 1
self._st.append('run(%s)' % str(done_cb))
self.done_cb = done_cb
state = self.state
if not self.preflight() or state.uri == None:
# generally a good sign that we're not going much further.
self.finish_task()
return
if 'user-agent' not in [i[0].lower() for i in state.req_hdrs]:
state.req_hdrs.append(
(u"User-Agent", u"RED/%s (http://redbot.org/)" % __version__))
self.exchange = self.client.exchange()
self.exchange.on('response_start', self._response_start)
self.exchange.on('response_body', self._response_body)
self.exchange.on('response_done', self._response_done)
self.exchange.on('error', self._response_error)
if self.status_cb and state.type:
self.status_cb("fetching %s (%s)" % (state.uri, state.type))
req_hdrs = [
(k.encode('ascii', 'replace'), v.encode('latin-1', 'replace')) \
for (k, v) in state.req_hdrs
]
self.exchange.request_start(state.method, state.uri, req_hdrs)
state.req_ts = thor.time()
if state.req_body != None:
self.exchange.request_body(state.req_body)
self.exchange.request_done([])
示例6: save_test
def save_test(self) -> None:
"""Save a previously run test_id."""
try:
# touch the save file so it isn't deleted.
os.utime(os.path.join(self.config.save_dir, self.test_id), (
thor.time(), thor.time() + (self.config.save_days * 24 * 60 * 60)))
location = "?id=%s" % self.test_id
if self.descend:
location = "%s&descend=True" % location
self.response_start("303", "See Other", [("Location", location)])
self.response_body("Redirecting to the saved test page...".encode(self.config.charset))
except (OSError, IOError):
self.response_start(b"500", b"Internal Server Error",
[(b"Content-Type", b"text/html; charset=%s" % self.charset_bytes),])
self.response_body(self.show_error("Sorry, I couldn't save that."))
self.response_done([])
示例7: read
def read(self):
"""
Read the file, returning its contents. If it does not exist or
cannot be read, returns None.
"""
if not path.exists(self.path):
return None
try:
fd = gzip.open(self.path)
except (OSError, IOError, zlib.error):
self.delete()
return None
try:
mtime = os.fstat(fd.fileno()).st_mtime
is_fresh = mtime > thor.time()
if not is_fresh:
self.delete()
return None
content = fd.read()
except IOError:
self.delete()
return None
finally:
fd.close()
return content
示例8: final_status
def final_status(self):
# See issue #51
# self.status("RED made %(reqs)s requests in %(elapse)2.3f seconds." % {
# 'reqs': fetch.total_requests,
self.status("RED finished in %(elapse)2.3f seconds." % {
'elapse': thor.time() - self.start
})
示例9: final_status
def final_status(self) -> None:
# See issue #51
# self.status("REDbot made %(reqs)s requests in %(elapse)2.3f seconds." % {
# 'reqs': fetch.total_requests,
self.status("")
self.output("""
<div id="final_status">%(elapse)2.2f seconds</div>
""" % {'elapse': thor.time() - self.start})
示例10: status
def status(self, message: str) -> None:
"Update the status bar of the browser"
self.output("""
<script>
<!-- %3.3f
$('#red_status').text("%s");
-->
</script>
""" % (thor.time() - self.start, e_html(message)))
示例11: debug
def debug(self, message: str) -> None:
"Debug to console."
self.output("""
<script>
<!--
console.log("%3.3f %s");
-->
</script>
""" % (thor.time() - self.start, e_js(message)))
示例12: status
def status(self, message):
"Update the status bar of the browser"
self.output(u"""
<script>
<!-- %3.3f
window.status="%s";
-->
</script>
""" % (thor.time() - self.start, e_html(message)))
示例13: load_saved_test
def load_saved_test(self):
"""Load a saved test by test_id."""
try:
fd = gzip.open(os.path.join(
save_dir, os.path.basename(self.test_id)
))
mtime = os.fstat(fd.fileno()).st_mtime
except (OSError, IOError, TypeError, zlib.error):
self.response_start(
"404", "Not Found", [
("Content-Type", "text/html; charset=%s" % charset),
("Cache-Control", "max-age=600, must-revalidate")
])
# TODO: better error page (through formatter?)
self.response_body(error_template %
"I'm sorry, I can't find that saved response."
)
self.response_done([])
return
is_saved = mtime > thor.time()
try:
state = pickle.load(fd)
except (pickle.PickleError, EOFError):
self.response_start(
"500", "Internal Server Error", [
("Content-Type", "text/html; charset=%s" % charset),
("Cache-Control", "max-age=600, must-revalidate")
])
# TODO: better error page (through formatter?)
self.response_body(error_template %
"I'm sorry, I had a problem reading that response."
)
self.response_done([])
return
finally:
fd.close()
formatter = find_formatter(self.format, 'html', self.descend)(
self.base_uri, state.request.uri, state.orig_req_hdrs, lang,
self.output, allow_save=(not is_saved), is_saved=True,
test_id=self.test_id
)
self.response_start(
"200", "OK", [
("Content-Type", "%s; charset=%s" % (
formatter.media_type, charset)),
("Cache-Control", "max-age=3600, must-revalidate")
])
if self.check_type:
# TODO: catch errors
state = state.subreqs.get(self.check_type, None)
formatter.start_output()
formatter.set_state(state)
formatter.finish_output()
self.response_done([])
示例14: _response_start
def _response_start(self, status, phrase, res_headers):
"Process the response start-line and headers."
self._st.append('_response_start(%s, %s)' % (status, phrase))
self.response.start_time = thor.time()
self.response.version = self.exchange.res_version
self.response.status_code = status.decode('iso-8859-1', 'replace')
self.response.status_phrase = phrase.decode('iso-8859-1', 'replace')
self.response.set_headers(res_headers)
StatusChecker(self.response, self.request)
checkCaching(self.response, self.request)
示例15: _response_done
def _response_done(self, trailers):
"Finish analysing the response, handling any parse errors."
self._st.append('_response_done()')
self.response.complete_time = thor.time()
self.response.transfer_length = self.exchange.input_transfer_length
self.response.header_length = self.exchange.input_header_length
self.response.body_done(True, trailers)
if self.status_cb and self.name:
self.status_cb("fetched %s (%s)" % (
self.request.uri, self.name
))
self.done()
self.finish_task()