Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34457

python 3.3 urllib.request

Date 2012-12-07 13:52 +0100
Subject python 3.3 urllib.request
From Steeve C <steevechailloux@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.605.1354884780.29569.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

hello,

I have a python3 script with urllib.request which have a strange behavior,
here is the script :

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import urllib.request
import sys, time


url = 'http://google.com'

def make_some_stuff(page, url):
    sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> page from \"") +
url + "\"\n")
    sys.stderr.write(str(page) + "\"\n")
    return True

def get_page(url):
    while 1:
        try:
            page = urllib.request.urlopen(url)
            yield page

        except urllib.error.URLError as e:
            sys.stderr.write(time.strftime("%d/%m/%Y %H:%M:%S -> impossible
to access to \"") + url + "\"\n")
            time.sleep(5)
            continue

def main():
    print('in main')
    for page in get_page(url):
        make_some_stuff(page, url)
        time.sleep(5)

if __name__ == '__main__':
    main()
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

if the computer is connected on internet (with an ethernet connection for
example) and I run this script, it works like a charme :
- urllib.request.urlopen return the page
- make_some_stuff write in stderr
- when the ethernet cable is unplug the except block handle the error while
the cable is unplug, and when the cable is pluged
back urllib.request.urlopen return the page and make_some_stuff write in
stderr

this is the normal behavior (for me, imho).

but if the computer is not connected on internet (ethernet cable unpluged)
and I run this script, the except block handle the error (normal), but when
I plug the cable, the script continue looping and urllib.request.urlopen
never return the page (so, it alway go to the except block)

What can I do to handle that ?

Thanks

Steeve

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

python 3.3 urllib.request Steeve C <steevechailloux@gmail.com> - 2012-12-07 13:52 +0100
  Re: python 3.3 urllib.request Hans Mulder <hansmu@xs4all.nl> - 2012-12-07 18:27 +0100
    Re: python 3.3 urllib.request Terry Reedy <tjreedy@udel.edu> - 2012-12-08 01:20 -0500
      Re: python 3.3 urllib.request Hans Mulder <hansmu@xs4all.nl> - 2012-12-08 13:21 +0100

csiph-web