1.Python hasattr() 函数
描述
hasattr() 函数用于判断对象是否包含对应的属性。
语法
hasattr 语法:
hasattr(object, name)
参数
- object -- 对象。
- name -- 字符串,属性名。
返回值
如果对象有该属性返回 True,否则返回 False。
实例
以下实例展示了 hasattr 的使用方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Coordinate: x = 10 y = -5 z = 0 point1 = Coordinate() print(hasattr(point1, 'x')) print(hasattr(point1, 'y')) print(hasattr(point1, 'z')) print(hasattr(point1, 'no')) # 没有该属性
输出结果:
True
True
True
False
打包.ui文件:
C:\Python27\Lib\site-packages\PyQt4>pyuic4 E:\pyDemo\proTools.ui -o proTools.py
Qt 代码是面向对象的,并且简单易学。所有我们添加的组件都是一个对象,并且都拥有自己的方法比如toPlainText()(用来读取输入框中的值)。这让它使用起来非常方便。
代码打包:C:\Python27\Scripts>python pyinstaller-script.py -w E:\pyDemo\PyQt\testPyQt.py testPyQt.spec
-F 带黑窗口 (打包成单个exe)
-W 不带黑窗口(打包成文件形式)
-i 加图标
pyinstaller -i D:\ALIX_SH\2carReport\yx.ico -W D:\ALIX_SH\2carReport\upload2carReport.py
-F -w 打包成exe,但不带黑窗口
pyinstaller -i D:\ALIX_SH\2carReport\yx.ico -F -w D:\ALIX_SH\2carReport\upload2carReport.py
下面用Pyinstaller生成一个可执行文件试试。
在命令提示符中去到要打包的目录;直接输入"pyinstaller -F -w 文件名.py(F大写);文件目录出现新的文件夹,里面就是打包好的exe文件了;
- -F, -onefile:打包成一个exe文件
- -w, -windowed:使用窗口,无控制台
- -c, -console:无窗口,使用控制台
- -D, -onedir:创建一个目录,包含exe文件,但会依赖其他文件
- --icon=图标路径
可以通过"Pyinstaller -h"来查看
Python发邮件的代码如下:
只需要填写好加粗字体,即可正常使用。
from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody def Email(to, subject, body): creds = Credentials( username='xxxxxx', password='xxxxxx' ) account = Account( primary_smtp_address='xxx@xxx.com', credentials=creds, autodiscover=True, access_type=DELEGATE ) m = Message( account=account, subject=subject, body=HTMLBody(body), to_recipients = [Mailbox(email_address=to)] ) m.send() Email("xxx@xxx.com", "abc", "def")
但是如果Python环境安装有瑕疵,则报错如下:
$python3 ab.py Traceback (most recent call last): File "ab.py", line 22, in <module> Email("xxx@xxx.com", "abc", "def") File "ab.py", line 12, in Email access_type=DELEGATE File "/usr/local/lib/python3.5/site-packages/exchangelib/account.py", line 66, in __init__ credentials=credentials) File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line 214, in discover email=email) File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line 236, in _try_autodiscover return _try_autodiscover(e.server, credentials, email) File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line 262, in _try_autodiscover raise_from(AutoDiscoverFailed('All steps in the autodiscover protocol failed'), None) File "/usr/local/lib/python3.5/site-packages/future/utils/__init__.py", line 398, in raise_from exec(execstr, myglobals, mylocals) File "<string>", line 1, in <module> exchangelib.errors.AutoDiscoverFailed: All steps in the autodiscover protocol failed
好在我有台机器安装Python3.5.2正常,执行上述完全没有问题。
今天终于搞定!!! 2017-12-11
exchange的版本不对,
我本机版本:
pip3 search exchangelib
exchangelib (1.10.6) - Client for Microsoft Exchange Web Services (EWS)
INSTALLED: 1.9.4
LATEST: 1.10.6
服务器版本为:
$pip3 search exchangelib
exchangelib (1.10.6) - Client for Microsoft Exchange Web Services (EWS)
INSTALLED: 1.10.4
LATEST: 1.10.6
要做的就是把服务器上的版本降低到1.9.4,就🆗了。
pip3 install exchangelib==1.9.4
再次执行发邮件的操作,bingo,搞定!
1、首先下载一个神器exchangelib,操作 pip install exchangelib
2、然后只需要将下面的信息改成你的信息就可以了
from exchangelib import Account, Message, Mailbox, HTMLBody, ServiceAccount #忽略警告 import urllib3 urllib3.disable_warnings() #报错处理:ConnectionError: HTTPConnectionPool(host='mail.xxx.com', port=443): Max retries exceeded with url: # import socket # socket.create_connection((host, port), timeout=10) def Email(to, subject, body): creds = ServiceAccount(username='域\名', password='密码') account = Account('发送的邮箱', credentials=creds, autodiscover=True) m = Message(account=account,subject=subject,body=HTMLBody(body), to_recipients = [Mailbox(email_address=to)] ) m.send() Email("接收的邮箱", "邮件标题", "邮件内容")
安装库exchangelib
pip install exchangelib1
脚本内容
# coding=utf-8 # # Created on 2018/2/ from exchangelib import DELEGATE, Account, Credentials, Configuration, NTLM, Message, Mailbox, HTMLBody from exchangelib.protocol import BaseProtocol, NoVerifyHTTPAdapter #此句用来消除ssl证书错误,exchange使用自签证书需加上 BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter # 输入你的域账号如example\leo cred = Credentials(r'EXAMPLE\leo', '输入你的密码') config = Configuration(server='输入邮箱服务器网页地址', credentials=cred, auth_type=NTLM) a = Account( primary_smtp_address='输入你要绑定的邮箱名(leo@example.com)', config=config, autodiscover=False, access_type=DELEGATE ) # 此处为用来发送html格式邮件的文件路径 with open(r'C:\Users\leo\Desktop\1.html') as f: msg = f.read().decode('utf-8') m = Message( account=a, folder=a.sent, subject=u'测试邮件', body=HTMLBody(msg), to_recipients=[Mailbox(email_address='输入你要绑定的邮箱名(leo@example.com)')] ) m.send_and_save()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/199107/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)