10.7. الوصول إلى الإنترنت
هناك عدد من الوحدات النمطية للوصول إلى الإنترنت ومعالجة بروتوكولات الإنترنت. اثنتان من أبسطها هما urllib.request لاسترجاع البيانات من عناوين URL وsmtplib لإرسال البريد:
>>> from urllib.request import urlopen
>>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
... for line in response:
... 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)
<BR>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.)