مشخصات مقاله
-
823
-
0.0
-
4775
-
0
-
0
ماژول های دسترسی به اینترنت در پایتون
آموزش دسترسی به اینترنت در برنامه نویسی پایتون
تعدادی ماژول برای دسترسی به اینترنت و پردازش پروتکل های اینترنت وجود دارد. دو تا از ساده ترین پروتکل ها عبارتند از urllib.request برای بازیابی داده از URL ها و smtplib برای ارسال پیام پستی(mail).
>>> 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)
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()
(توجه داشته باشید، مثال دوم نیاز به یک mailserver دارد که روی localhost اجرا شود.)