Python : 标准库-访问互联网

有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个是用于处理从 urls 接收的数据的 urllib.request 以及用于发送电子邮件的 smtplib:

from urllib.request import urlopen
for line in urlopen(‘http://tycho.usno.navy.mil/cgi-bin/timer.pl’):
… line = line.decode(‘utf-8’) # Decoding the binary data to text.
… if ‘EST’ in line or ‘EDT’ in line: # look for Eastern Time
… print(line)


Nov. 25, 09:43:32 PM EST

import smtplib
server = smtplib.SMTP(‘localhost’)
server.sendmail(‘[email protected]’, ‘[email protected]’,
… “”“To: [email protected]
… From: [email protected]

… Beware the Ides of March.
… “””)

server.quit()

注意第二个例子需要本地有一个在运行的邮件服务器。

猜你喜欢

转载自blog.csdn.net/weixin_44523387/article/details/92163761