Функция возвращает список строк, прочитанный из файла на удаленном сервере:
%%writefile url_open.py
import urllib.request
def url_open(url:str) -> list:
"""
Функция возвращает список строк, прочитанный из файла на удаленном сервере.
"""
lst = list()
with urllib.request.urlopen(url) as webpage:
# где webpage - файловый объект, кот. содержит информацию об открываемом файле.
for line in webpage:
# преобразуем тип bytes в str с исп. кодировки utf-8
lst.append(line.decode('utf-8').strip())
return lst
Writing url_open.py
Теперь можем импортировать модуль:
from url_open import url_open
print(url_open("http://dfedorov.spb.ru/python3/src/romeo.txt"))
['But soft what light through yonder window breaks', 'It is the east and Juliet is the sun', 'Arise fair sun and kill the envious moon', 'Who is already sick and pale with grief']