python案例源码_【python】python实例集一
#打開一個記事本
import os
os.startfile('notepad.exe')
#當前文件的根目錄
import os
print os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
#獲取本機IP
import socket
hostname = socket.gethostname()
print hostname
IPinfo = socket.gethostbyname_ex(hostname)
print IPinfo
LocalIP = IPinfo[2]
print LocalIP
#定時器執行一個命令
import os
import time
def print_ts(message):
print "[%s] %s"%(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), message)##time.strftime把時間格式化
def run(interval, command):
print_ts("-"*100)
print_ts("Command %s"%command)
print_ts("Starting every %s seconds."%interval)
print_ts("-"*100)
while True:
try:
# sleep for the remaining seconds of interval,http://www.sharejs.com
print_ts("Sleeping until %s..."%((time.ctime(time.time()+interval))))#time.ctime把時間戳變為年月日形式
time.sleep(interval)
print_ts("Starting command.")
# execute the command
status = os.system(command)
print_ts("-"*100)
print_ts("Command status = %s."%status)
except Exception, e:
print e
if __name__=="__main__":
interval = 600
command = r"ipconfig"
run(interval, command)
#正則表達式,讀取tomcat的日志并打印日期
import re
regx = "\d\d\d\d-\d\d-\d+"
f = open("c:\stdout.log","r")
i = 0
for str in f.readlines():
if re.search(regx,str):
Response.write(str+"
")
if i>10:break#由于是測試,只分析十行
i=i+1
f.close()
#輸出當前windows下的所有活動窗口名稱
from win32gui import *
titles = set()
def foo(hwnd,mouse):
if IsWindow(hwnd) and IsWindowEnabled(hwnd) and IsWindowVisible(hwnd):
titles.add(GetWindowText(hwnd))
EnumWindows(foo, 0)
lt = [t for t in titles if t]
lt.sort()
for t in lt:
print t
#定時監聽ip和端口
#-*- coding: gbk -*-
import socket,time
while 1:
file_obj = open('ip.txt')
for line in file_obj:
try:
sc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip = line.split()[0]
port = int(line.split()[1])
print ip,port
#設置超時時間(0.0)
sc.settimeout(2)
sc.connect((ip,port))
timenow=time.localtime()
datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow)
logstr="%s:%s 連接成功->%s \n" %(ip,port,datenow)
print logstr
sc.close()
except:
file = open("log.txt", "a")
timenow=time.localtime()
datenow = time.strftime('%Y-%m-%d %H:%M:%S', timenow)
logstr="%s:%s 連接失敗->%s \n" %(ip,port,datenow)
print logstr
file.write(logstr)
file.close()
print "sleep 10....."
time.sleep(10)
總結
以上是生活随笔為你收集整理的python案例源码_【python】python实例集一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server 文件规划 -位置规
- 下一篇: 发现几个常用的asp.net MVC H