python实现打开网页测试以及exe打包(修改)
来源:互联网 发布:unity3d导出3dmax模型 编辑:程序博客网 时间:2024/06/10 06:37
如题,本文实现的是在windows环境下python编写脚本实现打开测试的网站,作为运维人员,每天一个个打开网页实在繁琐,这里将其归合成一个脚本,并且打包成exe,方便在别的没有安装Python环境的机器上面运行
首先电脑要安装python和pywin32-218.win-amd64-py2.7.exe
我这里都是安装64位的,并且安装的都是2.7版本的
安装win32模块的时候可能会提示需要注册,不然安装的时候会找不到python模块的目录
我这里顺便上传个register.py的代码,只需要放在python的安装目录下执行一下就可以了
import sysfrom _winreg import *# tweak as necessaryversion = sys.version[:3]installpath = sys.prefixregpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % ( installpath, installpath, installpath)def RegisterPy(): print "begin RegisterPy " try: print "open key : %s" % regpath reg = OpenKey(HKEY_CURRENT_USER, regpath) except EnvironmentError as e: try: reg = CreateKey(HKEY_CURRENT_USER, regpath) SetValue(reg, installkey, REG_SZ, installpath) SetValue(reg, pythonkey, REG_SZ, pythonpath) CloseKey(reg) except: print "*** EXCEPT: Unable to register!" return print "--- Python", version, "is now registered!" return if (QueryValue(reg, installkey) == installpath and QueryValue(reg, pythonkey) == pythonpath): CloseKey(reg) print "=== Python", version, "is already registered!" return CloseKey(reg) print "*** ERROR:Unable to register!" print "*** REASON:You probably have another Python installation!"def UnRegisterPy(): # print "begin UnRegisterPy " try: print "open HKEY_CURRENT_USER key=%s" % (regpath) reg = OpenKey(HKEY_CURRENT_USER, regpath) # reg = OpenKey(HKEY_LOCAL_MACHINE, regpath) except EnvironmentError: print "*** Python not registered?!" return try: DeleteKey(reg, installkey) DeleteKey(reg, pythonkey) DeleteKey(HKEY_LOCAL_MACHINE, regpath) except: print "*** Unable to un-register!" else: print "--- Python", version, "is no longer registered!"if __name__ == "__main__": RegisterPy()
然后安装pywin32的时候下一步的时候会找到python的注册目录,然后直接下一步就安装完成了,安装的目录在python的Lib下面的site-spackages下,如果安装位数错了可以删掉这个目录下对应的pywin32文件夹再重装
下面首先给出Python代码
#!D:\Python27\python.exe# -*- coding: UTF-8 -*-import urllib2import webbrowserimport win32apiimport win32conimport timeimport ostitle = "警告!"content = "网站无法打开"firefoxcontent = "没有安装firefox或者firefox不是安装在D盘,请检查!"firefoxPath = ""#获取firefox浏览器的应用程序所在目录,浏览器必须安装在D盘!def search(path, name): for item in os.listdir(path): item_path = os.path.join(path, item) if os.path.isdir(item_path): search(item_path, name) elif os.path.isfile(item_path): if name == item: global firefoxPath firefoxPath = item_pathsearch("D:/","firefox.exe")if firefoxPath: webbrowser.register('firefox', None, webbrowser.BackgroundBrowser(firefoxPath)) url1 = "http://www.baidu.com" try: response = urllib2.urlopen(url1,timeout=20) except urllib2.HTTPError as e: # HTTPError 是URLError的子类,需要写在前面,否则会被覆盖 print('The server couldn\'t fulfill the request.') print('Error code: ', e.code) except urllib2.URLError as e: content = '该网站无法打开' win32api.MessageBox(0, content.decode('utf-8').encode('gbk'), title.decode('utf-8').encode('gbk') + url1, win32con.MB_OK) else: webbrowser.get('firefox').open(url1,new=2, autoraise=True) #百度 time.sleep(3) url2 = "http://www.sina.com" try: response = urllib2.urlopen(url2,timeout=20) except urllib2.HTTPError as e: # HTTPError 是URLError的子类,需要写在前面,否则会被覆盖 print('The server couldn\'t fulfill the request.') print('Error code: ', e.code) except urllib2.URLError as e: content = '该网站无法打开' win32api.MessageBox(0, content.decode('utf-8').encode('gbk'), title.decode('utf-8').encode('gbk') + url2, win32con.MB_OK) else: webbrowser.get('firefox').open(url2,new=2, autoraise=True) #新浪else: win32api.MessageBox(0, firefoxcontent.decode('utf-8').encode('gbk'), title.decode('utf-8').encode('gbk') ,win32con.MB_OK)
下面 给大家讲解一下代码:
最开头需要指明python的根目录
然后我用了一个递归的search方法去搜索D盘的所有目录,如果含有firefox.exe这个应用程序,则赋值给这个firefoxPath
由于search遍历的路径是D盘,所以如果代码移植到别的电脑上面,这个电脑也必须把火狐浏览器安装在D盘
search函数中(D:\\)这里两个\\是转义字符,表示一个\
webbrowser模块:
new:0就是同一浏览器打开,1是打开新的浏览器,2是打开浏览器tab
firefox:把火狐浏览器赋值给这个变量,名字可以随便取
下面我调用了urllib2模块的方法分别打开sina和baidu两个网站,具体url根据你需要修改,但是url必须是以http开头的,不然后面对网站不可访问的错误抛出处理的时候会有麻烦,然后我用到了urllib2中的HTTPError和URLError的错误抛出处理,在处理里面调用了windows的MessageBox方法,当网站不可访问的时候会弹出不可访问的提示框
程序写好,确保可以执行成功之后把他打包成exe文件,这里我用到了pyinstaller-2.0这个软件,下载下来是个zip压缩包,解压之后直接就可以用了,不用安装
把我们刚才写好的python代码文件拷到Pyinstaller目录下,我这里是webcheck.py文件
然后我们打开windows的cmd命令,切换到pyinstaller的目录下使用命令
python pyinstaller.py -F -p D:\Pyt
hon27;D:\Python27\Lib;D:\Python27\Lib\site-packages;D:\Python27\Lib\site-package
s\adodbapi;D:\Python27\Lib\site-packages\win32;D:\Python27\Lib\site-packages\win
32com;D:\Python27\Lib\site-packages\win32comext;D:\Python27\Lib\site-packages\se
tuptools;D:\Python27\Lib\site-packages\pip;D:\Python27\Lib\site-packages\isapi w
ebcheck-f.py
hon27;D:\Python27\Lib;D:\Python27\Lib\site-packages;D:\Python27\Lib\site-package
s\adodbapi;D:\Python27\Lib\site-packages\win32;D:\Python27\Lib\site-packages\win
32com;D:\Python27\Lib\site-packages\win32comext;D:\Python27\Lib\site-packages\se
tuptools;D:\Python27\Lib\site-packages\pip;D:\Python27\Lib\site-packages\isapi w
ebcheck-f.py
可执行的文件
其中 -F是只生成一个.exe文件,-p后面加打包时候搜索的python库,我这几乎加了所有的路径进去
可以看到可执行文件了
然后双击他看能不能执行吧
错误:
在把exe放到别的电脑上面运行报错(注意:用用cmd命令的方式运行该程序,不然的话出现闪退的情况就很难看到错误信息了)
这里我们要在该电脑上面开启该目录的权限
cacls "D:/System Volumn Information" /E /G administrator:D
cacls "D:/System Volumn Information" /E /G administrator:C
建议上面两行都输一遍
赋予指定用户打开C盘和D盘对这个目录的访问权限
然后再用cmd打开貌似搞定了
最后注明:上面代码我在win7和win10的环境下测试经过调试都是可以运行的,win server 2008貌似是不行的
阅读全文
0 0
- python实现打开网页测试以及exe打包(修改)
- 网页中打开exe
- 简单Python实例,以及打包成.exe文件
- python打包(.py转换exe)
- python打包成exe
- Python 打包成 EXE
- python打包成exe
- Python打包成exe
- python打包exe程序
- python打包exe
- python 打包成EXE
- python打包成exe
- python打包exe
- python打包exe
- Python打包成exe
- Python打包成exe
- Python 打包exe
- python打包exe可执行文件
- 三级联动
- win10 +cuda8.0+pycuda+vs2015
- c++ 内联函数 (写的真好!!!)
- guava-cache深入了解
- 一个异常:Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to
- python实现打开网页测试以及exe打包(修改)
- 图解二叉树非递归版的中序遍历算法
- 拦截器
- 习题1.6
- 卫星控制天气,还记得以前的太极圈的下暴雨,闪电风暴天空,卫星控制天气和红警中是一样的。
- pycharm软件安装及环境配置
- CF 833A(Automatic Door-计算题)
- Your password does not satisfy the current policy requirements解决办法
- MQTT Server搭建(apache-apollo)和MQtt Client搭建