首页 文章 精选 留言 我的

精选列表

搜索[python],共10001篇文章
优秀的个人博客,低调大师

Python发送邮件

发送邮件的Demo: 使用smtp服务必须在相应邮箱服务器设置中开启SMTP服务,并且生成授权码 例如QQ: import smtplib from email.mime.text import MIMEText #第三方SMTP服务 mail_host = "smtp.qq.com" mail_user = "wsyjlly@foxmail.com" mail_pass = "授权码" sender = "wsyjlly@foxmail.com" receiver = "wsyjlly@foxmail.com" message = MIMEText("你好,世界!") message["From"] = sender message["To"] = receiver message["Subject"] = "一起嗨吧!" def send_email(): try: server = smtplib.SMTP() server.connect(mail_host) server.login(mail_user,mail_pass) server.sendmail(sender,receiver,message.as_string()) server.close() print("邮件发送成功!") except Exception as e: print("邮件发送失败!",e) if __name__ == '__main__': send_email()

优秀的个人博客,低调大师

python paramiko CryptographyDeprecationWarning

import paramiko client = paramiko.SSHClient() client.connect(serverIp, port=serverPort, username=serverUser) 报警告如下: paramiko\ecdsakey.py:164: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point self.ecdsa_curve.curve_class(), pointinfo paramiko\kex_ecdh_nist.py:39: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding. m.add_string(self.Q_C.public_numbers().encode_point()) paramiko\kex_ecdh_nist.py:96: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point self.curve, Q_S_bytes paramiko\kex_ecdh_nist.py:111: CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding. hm.add_string(self.Q_C.public_numbers().encode_point()) 原因 paramiko 2.4.2 依赖 cryptography,而最新的cryptography==2.5里有一些弃用的API。 解决 删掉cryptography 2.5,安装2.4.2,就不会报错了。 pip uninstall cryptography==2.5 pip install cryptography==2.4.2 补充 paramiko的issue #1369提到了这个问题,并已有PR #1379了,尚未合并。

优秀的个人博客,低调大师

Python requests库

requests是一个用于在程序中进行http协议下的get和post请求的库。 安装 easy_install requests 或者用 pip install requests 安装好之后在交互模式下运行 >>> import requests >>> dir(requests) ['ConnectionError', 'HTTPError', 'NullHandler', 'PreparedRequest', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'adapters', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'utils'] 从上面的列表中可以看出在http中常用到的getcookiespost等都赫然在目。 get请求 >>> r = requests.get("http://www.hiekay.com") 得到一个请求的实例然后 >>> r.cookies <RequestsCookieJar[]> 这个网站对客户端没有写任何cookies内容。换一个看看 >>> r = requests.get("http://www.baidu.com") >>> r.cookies <<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='PHPSESSID', value='buqj70k7f9rrg51emsvatveda2', port=None, port_specified=False, domain='www.baidu.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]> 原来这样呀。继续还有别的属性可以看看。 >>> r.headers {'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Server': 'bfe/1.0.8.18', 'Last-Modified': 'Mon, 23 Jan 2017 13:28:24 GMT', 'Connection': 'Keep-Alive', 'Pragma': 'no-cache', 'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Date': 'Wed, 16 Jan 2019 07:42:13 GMT', 'Content-Type': 'text/html'} >>> r.encoding 'UTF-8' >>> r.status_code 200 下面这个比较长是网页的内容仅仅截取显示部分 >>> print r.text <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>ç¾åº¦ä¸ä¸ï¼ä½å°±ç¥é</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=ç¾åº¦ä¸ä¸ class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>æ°é»</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>å°å¾</a> ...... 请求发出后requests会基于http头部对相应的编码做出有根据的推测当你访问r.text之时requests会使用其推测的文本编码。你可以找出requests使用了什么编码并且能够使用r.coding属性来改变它。 >>> r.content '<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> ...... 以二进制的方式打开服务器并返回数据。 post请求 requests发送post请求通常你会想要发送一些编码为表单的数据——非常像一个html表单。要实现这个只需要简单地传递一个字典给data参数。你的数据字典在发出请求时会自动编码为表单形式。 >>> import requests >>> payload = {"key1":"value1","key2":"value2"} >>> r = requests.post("http://httpbin.org/post") >>> r1 = requests.post("http://httpbin.org/post", data=payload) 看看效果 http头部 >>> r.headers['content-type'] 'application/json' 注意在引号里面的内容不区分大小写'CONTENT-TYPE'也可以。 还能够自定义头部 >>> r.headers['content-type'] = 'adad' >>> r.headers['content-type'] 'adad' 注意当定制头部的时候如果需要定制的项目有很多需要用到数据类型为字典。 * 备注 网上有一个更为详细叙述有关requests模块的网页可以参考http://requests-docs-cn.readthedocs.org/zh_CN/latest/index.html

优秀的个人博客,低调大师

python 文件处理

模式: 读'r' 写[清空整个文件]'w' 追加[文件需要存在]'a' 读写'r+' 二进制文件'b' 'rb','wb','rb+' 写文件 i={'ddd':'ccc'} f = file('poem.txt', 'a') f.write("string") f.write(str(i)) f.flush() f.close() 读文件 f = file('/etc/passwd','r') c = f.read().strip() # 读取为一个大字符串,并去掉最后一个换行符 for i in c.spilt('\n'): # 用换行符切割字符串得到列表循环每行 print i f.close() 读文件2 > for i in open('b.txt'): # 直接读取也可迭代,并有利于大文件读取,但不可反复读取 print i, 追加日志 > log = open('/home/peterli/xuesong','a') print >> log,'faaa' log.close() with读文件 with open('a.txt') as f: for i in f: print i print f.read() # 打印所有内容为字符串 print f.readlines() 文件随机读写 # 文件本没有换行,一切都是字符,文件也没有插入功能 f.tell() # 当前读写位置 f.read(5) # 读取5个字符并改变指针 f.seek(5) # 改变用户态读写指针偏移位置,可做随机写 f.seek(p,0) # 移动当文件第p个字节处,绝对位置 f.seek(p,1) # 移动到相对于当前位置之后的p个字节 f.seek(p,2) # 移动到相对文件尾之后的p个字节 f.seek(0,2) # 指针指到尾部 # 改变指针超出文件尾部,会造成文件洞,ll看占用较大,但du -sh却非常小 f.read(65535) # 读取64K字节 f.write("str") # 写会覆盖当前指针后的响应字符,无插入功能

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册