码迷,mamicode.com
首页 > 编程语言 > 详细

python 统计时间,写日志

时间:2017-03-19 03:07:13      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:模块   test   eth   import   deb   stream   log   erro   time   

python 统计时间使用time模块,写日志使用logging模块,这两个都是标准模板。

测试socket使用socket模块

 

# 统计时间

----------------------

import time

 

start = time.time()

end = time.time()

stamp = end - start

print "耗时", stamp

 

# 日志

-----------------------

import logging
import datetime

curDate = datetime.date.today() - datetime.timedelta(days=0)
logName = ‘d:\\py_error_%s.log‘ %curDate

logging.basicConfig(level=logging.INFO,
format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘,
#datefmt=‘%a, %d %b %Y %H:%M:%S‘,
filename=logName,
filemode=‘a‘)

logging.warn("warn------")
logging.debug("debug")
logging.info("info")
logging.error("error")

 

# socket测试

-----------------------------------

import socket
import sys
import time

def socket_test():
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = "baidu.com"
port = 80

ip = socket.gethostbyname(host)

s.connect((ip,port))
message = "GET / HTTP/1.1\r\nHost:" + host + "\r\n\r\n"

try:
s.sendall(message)
except socket.error:
print "Send failed"
sys.exit()

print "message send success"

reply = s.recv(4096)
s.close()
print reply

if __name__ == ‘__main__‘:

# 测试次数
count = 10
start = time.time()
for i in range(0,count):
#print i
socket_test()
end = time.time()
stamp = end - start
print "----------------------"
print "总耗时",stamp,"单次耗时",stamp/count

python 统计时间,写日志

标签:模块   test   eth   import   deb   stream   log   erro   time   

原文地址:http://www.cnblogs.com/jiftle/p/6577074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!