Thread View: pl.comp.lang.python
7 messages
7 total messages
Started by Desper0
Wed, 12 Jan 2011 23:18
pobieranie pliku + autoryzacja
Author: Desper0
Date: Wed, 12 Jan 2011 23:18
Date: Wed, 12 Jan 2011 23:18
42 lines
2800 bytes
2800 bytes
Witam,
chciałbym napisac skrypt (jeden z pierwszych w Pythonie ), który
umożliwiłby mi pobranie pliku xml ze strony wymagającej podania
loginu i hasła,
nie ma problemu aby napisać to np. w PHP uzyc wgeta :
wget --http-user=login" -O "data.xml" --http-passwd=haslo
http://www.sss.com/Log/ProxyXML.aspx?type=xml
i napisać parsera, ale chciałbym to napisac w pythonie, którego
chciałbym poznać lepiej.
tak wygląda cały skrypt: Python 3.1 MS Windows XP
-------------------8<--------------------------------------------------------
import urllib.request
url2='http://www.sss.com/Log/ProxyXML.aspx?type=xml'
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url2, 'user_name', 'password')
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
opener = urllib.request.build_opener(handler)
opener.open(url2)
urllib.request.install_opener(opener)
req = urllib.request.Request(url2)
try: response = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e)
the_page = response.read()
---------------------------------------8<-----------------------------------------------------
Oraz komunikat błędu:
HTTP Error 401: Unauthorized
Traceback (most recent call last):
File "D:\workspace\parsXml\src\parser.py", line 40, in <module>
the_page = response.read()
NameError: name 'response' is not defined
Wciąż brak autoryzacji,
w czym może tkwić problem ?
Pisząc skrypt używałem dokumentacji do wersji 3.x
http://docs.activestate.com/activepython/3.1/python/howto/urllib2.html
TIA
Re: pobieranie pliku + autoryzacja
Author: Trimar
Date: Thu, 13 Jan 2011 08:34
Date: Thu, 13 Jan 2011 08:34
15 lines
1638 bytes
1638 bytes
W dniu 2011-01-13 00:18, Desper0 pisze:
> -------------------8<--------------------------------------------------------
> Oraz komunikat błędu:
> HTTP Error 401: Unauthorized
> Traceback (most recent call last):
> File "D:\workspace\parsXml\src\parser.py", line 40, in<module>
> the_page = response.read()
> NameError: name 'response' is not defined
Witam
Może zobacz tak
authentication_handler=urllib.HTTPBasicAuthHandler(urllib.HTTPPasswordMgrWithDefaultRealm())
authentication_handler.add_password(None,url2,'user','password')
auth_opener = urllib.build_opener(authentication_handler)
urllib.install_opener(auth_opener)
req = urllib.Request(url2)
Re: pobieranie pliku + autoryzacja
Author: Desper0
Date: Thu, 13 Jan 2011 13:26
Date: Thu, 13 Jan 2011 13:26
19 lines
2012 bytes
2012 bytes
On 13 Sty, 09:34, Trimar <trima...@poczta.onet.pl> wrote: > W dniu 2011-01-13 00:18, Desper0 pisze:> -------------------8<-------------------------------------------------------- > > Oraz komunikat b du: > > HTTP Error 401: Unauthorized > > Traceback (most recent call last): > > File "D:\workspace\parsXml\src\parser.py", line 40, in<module> > > the_page = response.read() > > NameError: name 'response' is not defined > > Witam > Mo e zobacz tak > authentication_handler=urllib.HTTPBasicAuthHandler(urllib.HTTPPasswordMgrWithDefaultRealm()) > authentication_handler.add_password(None,url2,'user','password') > auth_opener = urllib.build_opener(authentication_handler) > urllib.install_opener(auth_opener) > req = urllib.Request(url2) Niestety, efekt ten sam :401
Re: pobieranie pliku + autoryzacja
Author: Trimar
Date: Thu, 13 Jan 2011 14:59
Date: Thu, 13 Jan 2011 14:59
19 lines
1645 bytes
1645 bytes
W dniu 2011-01-13 14:26, Desper0 pisze: >> Mo e zobacz tak >> authentication_handler=urllib.HTTPBasicAuthHandler(urllib.HTTPPasswordMgrWithDefaultRealm()) >> authentication_handler.add_password(None,url2,'user','password') >> auth_opener = urllib.build_opener(authentication_handler) >> urllib.install_opener(auth_opener) >> req = urllib.Request(url2) > > Niestety, > efekt ten sam :401 A możesz dodać coś takiego import httplib httplib.HTTPConnection.debuglevel = 1 Zobaczymy co tam się majta po http i może da się coś poradzić Pozdrawiam Trimar
Re: pobieranie pliku + autoryzacja
Author: Desper0
Date: Thu, 13 Jan 2011 15:23
Date: Thu, 13 Jan 2011 15:23
38 lines
2887 bytes
2887 bytes
On 13 Sty, 15:59, Trimar <trima...@poczta.onet.pl> wrote: > W dniu 2011-01-13 14:26, Desper0 pisze: > > >> Mo e zobacz tak > >> authentication_handler=urllib.HTTPBasicAuthHandler(urllib.HTTPPasswordMgrWithDefaultRealm()) > >> authentication_handler.add_password(None,url2,'user','password') > >> auth_opener = urllib.build_opener(authentication_handler) > >> urllib.install_opener(auth_opener) > >> req = urllib.Request(url2) > > > Niestety, > > efekt ten sam :401 > > A możesz dodać coś takiego > > import httplib > httplib.HTTPConnection.debuglevel = 1 > > Zobaczymy co tam się majta po http i może da się coś poradzić > Pozdrawiam > Trimar Odpowiedz z debugowania: send: b'GET /Log/ProxyXML.aspx?type=xml HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: sss.com\r\nConnection: close\r\nUser-Agent: Python- urllib/3.1\r\n\r\n' reply: 'HTTP/1.1 302 Found\r\n' header: Connection header: Date header: Server header: ETag header: X- Powered-By header: X-AspNet-Version header: Location header: Set- Cookie header: Cache-Control header: Pragma header: Expires header: Content-Type header: Content-Length send: b'GET /Export_XML.aspx HTTP/ 1.1\r\nAccept-Encoding: identity\r\nHost: TUTAJ_ADRES_IP\r \nConnection: close\r\nUser-Agent: Python-urllib/3.1\r\n\r\n' reply: 'HTTP/1.1 401 Unauthorized\r\n' header: Connection header: Date header: Server header: ETag header: X- Powered-By header: X-AspNet-Version header: WWW-Authenticate header: Cache-Control header: Content-Type header: Content-Length HTTP Error 401: Unauthorized
Re: pobieranie pliku + autoryzacja
Author: Trimar
Date: Sun, 16 Jan 2011 15:14
Date: Sun, 16 Jan 2011 15:14
18 lines
1911 bytes
1911 bytes
W dniu 2011-01-13 16:23, Desper0 pisze: > On 13 Sty, 15:59, Trimar<trima...@poczta.onet.pl> wrote: >> W dniu 2011-01-13 14:26, Desper0 pisze: >> >>>> Mo e zobacz tak >>>> authentication_handler=urllib.HTTPBasicAuthHandler(urllib.HTTPPasswordMgrWithDefaultRealm()) >>>> authentication_handler.add_password(None,url2,'user','password') >>>> auth_opener = urllib.build_opener(authentication_handler) >>>> urllib.install_opener(auth_opener) >>>> req = urllib.Request(url2) >> > reply: 'HTTP/1.1 302 Found\r\n' OK jest redirect wiec zamiast url2 w authentication_handler.add_password(None,url2,'user','password') daj authentication_handler.add_password(None,'hrrp://www.sss.com','user','password') i powinno działać Trimar
Re: pobieranie pliku + autoryzacja
Author: Artur M. Piwko
Date: Thu, 20 Jan 2011 07:55
Date: Thu, 20 Jan 2011 07:55
34 lines
1873 bytes
1873 bytes
In the darkest hour on Wed, 12 Jan 2011 15:18:29 -0800 (PST),
Desper0 <ansic.soft@gmail.com> screamed:
> Witam,
> chciałbym napisac skrypt (jeden z pierwszych w Pythonie ), który
> umożliwiłby mi pobranie pliku xml ze strony wymagającej podania
> loginu i hasła,
> nie ma problemu aby napisać to np. w PHP uzyc wgeta :
> wget --http-user=login" -O "data.xml" --http-passwd=haslo
> http://www.sss.com/Log/ProxyXML.aspx?type=xml
>
> i napisać parsera, ale chciałbym to napisac w pythonie, którego
> chciałbym poznać lepiej.
>
(Nietestowane.)
import mechanize
URI = 'http://www.sss.com/Log/ProxyXML.aspx?type=xml'
USER = 'user'
PASSWD = 'passwd'
browser = mechanize.Browser()
browser.add_password(URI, USER, PASSWD')
response = browser.response()
print response.read()
--
[ Artur M. Piwko : Pipen : AMP29-RIPE : RLU:100918 : From == Trap! : SIG:222B ]
[ 08:51:36 user up 12688 days, 20:46, 1 user, load average: 0.09, 0.89, 0.54 ]
To clear screen, press [Ctrl] [Alt] [Del].
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads