牛骨文教育服务平台(让学习变的简单)

有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个是用于处理从 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("soothsayer@example.org", "jcaesar@example.org",
... """To: jcaesar@example.org
... From: soothsayer@example.org
...
... Beware the Ides of March.
... """)
>>> server.quit()


(注意第二个例子需要在 localhost 运行一个邮件服务器。)