概览
我们使用Python调用HttpWatch自动化接口来记录和分析HTTP流量,通过使用IE加载一个简单的页面。
程序操作
这个程序通过让用户输入一个网页的URL来监测。如果用户只是简单的敲了回车键,则使用HttpWatch的主页。
得到URL之后,第一步就是创建Controller的实例,然后使用它创建IE的实例。注意这个Controller类的IE属性,它返回一个IE对象的引用,可以提供方法来创建HttpWatch插件和浏览器实例。
import win32com.client
# Create a new instance of HttpWatch in IE
control = win32com.client.Dispatch('HttpWatch.Controller')
plugin = control.IE.New()
#Creating a New Instance of the HttpWatch Plug-in in IE
plugin.Log.EnableFilter(false)
plugin.Record()
我们现在可以启动录制HTTP请求和响应。 首先通过调研日志对象的EnableFilter方法把Filtering disable掉,然后调用插件的Record方法来开始捕捉流量。
#Start Recording HTTP Traffic
plugin.Log.EnableFilter(false)
plugin.Record()
载入URL
Wait的参数-1,指可以无限期地等下去
plugin.GotoURL(url)
control.Wait(plugin, -1)
展示页面统计信息
if plugin.Log.Pages.Count != 0 :
print "nPage Title: '", plugin.Log.Pages(0).Title , "'"
# Display summary statistics for page
summary = plugin.Log.Pages(0).Entries.Summary
print "Total time to load page (secs): ", summary.Time
print "Number of bytes received on network: ", summary.BytesReceived
print "HTTP compression saving (bytes): ", summary.CompressionSavedBytes
print "Number of round trips: ", summary.RoundTrips
print "Number of errors: ", summary.Errors.Count