python中的logging模块应用
python中有系统自带的logging模块,使用起来非常方便,并且要在程序中经常要使用这个模块,这样出了问题可以通过日志很方便的查找在哪里出了问题,比直接找代码要方便些
我记录了一些经常用到的,不是很常用的可以到python文档中去查看 http://docs.python.org/2/library/logging.html
python中有系统自带的logging模块,使用起来非常方便,并且要在程序中经常要使用这个模块,这样出了问题可以通过日志很方便的查找在哪里出了问题,比直接找代码要方便些
我记录了一些经常用到的,不是很常用的可以到python文档中去查看 http://docs.python.org/2/library/logging.html
网页中的数据在传递的时候有GET和POST两种方式,GET是以网址的形式传参数,在web.py中有着很好的匹配,如果我们配置以下的urls
urls =(
'/','index',
'/weixin/(.*?)','WeixinInterface'
先不考虑/weixin/后面的东西,现在我们来写index的类
class index:
def GET(self):
i = web.input(name = 'kevinkelin',age = 100)
这两天开始接触pywinauto,听说百度的自动化QA也用这个模块,于是来了兴趣,但网上的教程很少,而且基本上都是拿官方的notepad来说,首先中文菜单的支持是问题,其次各种操作也没有写清楚,阅读官方的文档,发现这个东西使用起来还真是非常方便,下面我也以notepad为例来说明一下它的简单操作。
https://sourceforge.net/project/showfiles.php?group_id=157379
http://www.rutherfurd.net/python/sendkeys/index.html
检测你是否安装正确
from pywinauto import application app = application.Application.start("notepad.exe") app.notepad.TypeKeys("%FX")
最近开始学习整理python在windows自动化测试中的使用,觉得挺有意思的
主要思路,在windows下,主要通过启进程,然后查找进程的句柄,然后再操作这个句柄,包括点击,填写文字,关闭,获取文字等操作
下面以一个简单的校验文件md5值的操作来介绍一个python的应用,当然python中有校验md5的函数,不用非要使用工具来校验,这里只是练习使用python来自动化操作
所用的工具有SpyLite,用于查看窗口ID,句柄等信息
今天研究了下urllib2这个库的使用,才发现以前有很多不明白的东西,现在写下来也做个记录
import urllib2
url = r'http://www.baidu.com'
html = urllib2.urlopen(url).read()
客户端与服务器端通过request与response来沟通,客户端先向服务端发送request,然后接收服务端返回的response
urllib2提供了request的类,可以让用户在发送请求前先构造一个request的对象,然后通过urllib2.urlopen方法来发送请求
import urllib2
url = r'http://www.baidu.com'
req = urllib2.Request(url)
html = urllib2.urlopen(req).read()
几乎所有的搜索引擎都提供联想词提示,这个小脚本其实功能性不大,只当作使用urllib2发http请求和如何使用代理来访问
参考文章
相对于python解析XML来说,我还是比较喜欢json的格式返回,现在一般的api返回都会有json与XML格式的选择,json的解析起来个人觉得相对简单些
先看一个简单的豆瓣的图书查询的api返回
解析XML主要用到pytohn自带的XML库,其次还是lxml库
先以一个相对简单但功能比较全的XML文档为例
<?xml version='1.0' encoding='utf-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xml:lang='en'>
<title>dive into mark</title>
<subtitle>currently between addictions</subtitle>
<id>tag:diveintomark.org,2001-07-29:/</id>
<updated>2009-03-27T21:56:07Z</updated>
<link rel='alternate' type='text/html' href='http://diveintomark.org/'/>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Dive into history, 2009 edition</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition'/>
<id>tag:diveintomark.org,2009-03-27:/archives/20090327172042</id>
<updated>2009-03-27T21:56:07Z</updated>
<published>2009-03-27T17:20:42Z</published>
<category scheme='http://diveintomark.org' term='diveintopython'/>
<category scheme='http://diveintomark.org' term='docbook'/>
<category scheme='http://diveintomark.org' term='html'/>
<summary type='html'>Putting an entire chapter on one page sounds
bloated, but consider this — my longest chapter so far
would be 75 printed pages, and it loads in under 5 seconds…
On dialup.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
<uri>http://diveintomark.org/</uri>
</author>
<title>Accessibility is a harsh mistress</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2009/03/21/accessibility-is-a-harsh-mistress'/>
<id>tag:diveintomark.org,2009-03-21:/archives/20090321200928</id>
<updated>2009-03-22T01:05:37Z</updated>
<published>2009-03-21T20:09:28Z</published>
<category scheme='http://diveintomark.org' term='accessibility'/>
<summary type='html'>The accessibility orthodoxy does not permit people to
question the value of features that are rarely useful and rarely used.</summary>
</entry>
<entry>
<author>
<name>Mark</name>
</author>
<title>A gentle introduction to video encoding, part 1: container formats</title>
<link rel='alternate' type='text/html'
href='http://diveintomark.org/archives/2008/12/18/give-part-1-container-formats'/>
<id>tag:diveintomark.org,2008-12-18:/archives/20081218155422</id>
<updated>2009-01-11T19:39:22Z</updated>
<published>2008-12-18T15:54:22Z</published>
<category scheme='http://diveintomark.org' term='asf'/>
<category scheme='http://diveintomark.org' term='avi'/>
<category scheme='http://diveintomark.org' term='encoding'/>
<category scheme='http://diveintomark.org' term='flv'/>
<category scheme='http://diveintomark.org' term='GIVE'/>
<category scheme='http://diveintomark.org' term='mp4'/>
<category scheme='http://diveintomark.org' term='ogg'/>
<category scheme='http://diveintomark.org' term='video'/>
<summary type='html'>These notes will eventually become part of a
tech talk on video encoding.</summary>
</entry>
</feed>
先简单的看一下这个XML的结构
最近无聊,想玩玩微信的公众平台,后来发现乐趣无穷啊~
使用的工具,python 新浪SAE平台,微信的公众平台
你需要先在微信的公众平台与新浪SAE平台上各种注册,微信平台注册的时候需要你拍张手持身份证的照片,还有几天的审核期
微信公众平台:http://mp.weixin.qq.com
利用python的smtplib,发邮件将是一件非常简单的事情,下文以用163邮箱来发邮件为例,说明smtplib的应用
#coding:utf-8
import smtplib
def sendMail(mail_to):
mail_server = 'smtp.163.com'
mail_port = '25'
username = 'soar_1987@163.com'
password = 'XXXXXXXX'
mail_title = 'python Test'
mail_content = 'This is a test from python for sending email'
if type(mail_to) == str:#之所以要有这样的判断是为了收件人是多个人或者传入的的收件人列表是以list的方式
mail_list = mail_to.split(';') #将str类型的数据转换为list
elif type(mail_to) == list:
mail_list = mail_to
else:
print "你输入的收件人格式有误"
try:
handle = smtplib.SMTP(mail_server,mail_port)
handle.login(username,password)
msg = "From:%srn To:%srnContent-Type: text/html;charset=gb2312rnSubject:%srnrn %s"%("杨彦星",";".join(mail_list),mail_title,mail_content) #这里的msg其实就是一种固定的格式,From:To:Subject
handle.sendmail(username,mail_list,msg)
print "Send email sucess"
except Exception,e:
print "Send email failed because %s" % e
if __name__ =="__main__":
sendMail('yanxingyang@gmail.com')