Zanurkuj w Pythonie/Źródła/openanything.py: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
tłumaczenie
Linia 1:
<source lang='python'>
'''u"""OpenAnything: amała kindbiblioteka and thoughtful library fordla HTTP web services
 
Ten program jest częścią książki "Zanurkuj w Pythonie", podręcznika
This program is part of 'Dive Into Python', a free Python book for
o Pythonie dla doświadczonych programistów. Najnowszą wersję można
experienced programmers. Visit http://diveintopython.org/ for the
znaleźć tu: http://pl.wikibooks.org/wiki/Zanurkuj_w_Pythonie.
latest version.
'''
Program ten został oparty na przykładach zawartych w książce
"Dive Into Python", a dostępnej stąd: http://www.diveintopython.org.
"""
 
__author__ = 'Mark Pilgrim (mark@diveintopython.org)'
Linia 39 ⟶ 42:
 
def openAnything(source, etag=None, lastmodified=None, agent=USER_AGENT):
u"""URL, filename,nazwa pliku lub orłańcuch stringznaków --> streamstrumień
 
Funkcja ta pozwala tworzyć parsery, które przyjmują jakieś źródło wejścia
This function lets you define parsers that take any input source
(URL, pathnameścieżkę todo localpliku orlokalnego networklub file,gdzieś orw actualsieci datalub asdane aw stringpostaci łańcucha znaków),
a następnie zaznajamia się z nim w odpowiedni sposób. Zwracany obiekt będzie
and deal with it in a uniform manner. Returned object is guaranteed
toposiadał havewszystkie allpodstawowe themetody basicczytania stdio read methodswejścia (read, readline, readlines).
JustPonadto korzystamy z .close(), thegdy objectobiekt whenjuż you'renam donenie withbędzie itpotrzebny.
 
IfKiedy thezostanie etagpodany argument is suppliedetag, it will be used aszostanie theon valuewykorzystany ofjako anwartość
nagłówka żądania URL-a If-None-Match request header.
 
IfJeśli theargument lastmodified argumentzostanie is suppliedpodany, it mustmusi bebyć aon formattedformie
łańcucha znaków określającego czas i datę w GMT.
date/time string in GMT (as returned in the Last-Modified header of
Data i czas sformatowana w tym łańcuchu zostanie wykorzystana
a previous request). The formatted date/time will be used
asjako thewartość valuenagłówka of anżądania If-Modified-Since request header.
 
Jeśli argument agent zostanie określony, będzie on wykorzystany
If the agent argument is supplied, it will be used as the value of a
w nagłówku żądania User-Agent request header.
"""
 
Linia 66 ⟶ 69:
 
if urlparse.urlparse(source)[0] == 'http':
# openotwiera URL withza pomocą urllib2
request = urllib2.Request(source)
request.add_header('User-Agent', agent)
Linia 77 ⟶ 80:
return opener.open(request)
# trypróbuje tootworzyć openza withpomocą nativewbudowanej funkcji open function (ifjeśli source isto anazwa filenamepliku)
try:
return open(source)
Linia 83 ⟶ 86:
pass
 
# treattraktuje source asjak stringłańcuch znaków
return StringIO(str(source))
 
def fetch(source, etag=None, lastmodified=None, agent=USER_AGENT):
u"""Pobiera dane z URL, pliku, strumienia lub łańcucha znaków"""
'''Fetch data and metadata from a URL, file, stream, or string'''
result = {}
f = openAnything(source, etag, lastmodified, agent)
result['data'] = f.read()
if hasattr(f, 'headers'):
# savezapisuje ETag, ifjeśli thego serverwysłał sentdo onenas serwer
result['etag'] = f.headers.get('ETag')
# savezapisuje nagłówek Last-Modified header, ifjeśli thezostał serverdo sentnas onewysłany
result['lastmodified'] = f.headers.get('Last-Modified')
if f.headers.get('content-encoding') == 'gzip':
# odkompresowuje otrzymane dane, ponieważ są one zakompresowane jako gzip
# data came back gzip-compressed, decompress it
result['data'] = gzip.GzipFile(fileobj=StringIO(result['data'])).read()
if hasattr(f, 'url'):
Linia 107 ⟶ 110:
return result
 
</source>
[[Kategoria:Zanurkuj w Pythonie/Źródła|openanything.py]]