473,721 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to prevent the script from stopping before it should

I have a script that downloads some webpages.The problem is that,
sometimes, after I download few pages the script hangs( stops).
(But sometimes it finishes in an excellent way ( to the end) and
download all the pages I want to)
I think the script stops if the internet connection to the server (from
where I download the pages) is rather poor.
Is there a solution how to prevent the script from hanging before all
pages are downloaded?

Thanks for help
Lad.

Jul 18 '05 #1
7 1792
#import urllib, sys
#pages = ['http://www.python.org' , 'http://xxx']
#for i in pages:
# try:
# u = urllib.urlopen( i)
# print u.geturl()
# except Exception, e:
# print >> sys.stderr, '%s: %s' % (e.__class__.__ name__, e)
will print an error if a page fails opening, rest opens fine

Jul 18 '05 #2
wi******@hotmai l.com wrote:
#import urllib, sys
#pages = ['http://www.python.org' , 'http://xxx']
#for i in pages:
# try:
# u = urllib.urlopen( i)
# print u.geturl()
# except Exception, e:
# print >> sys.stderr, '%s: %s' % (e.__class__.__ name__, e)
will print an error if a page fails opening, rest opens fine

More generally you may wish to use the timeout features of TCP sockets.
These were introduced in Python 2.3, though Tim O'Malley's module
"timeoutsoc ket" (which was the inspiration for the 2.3 upgrade) was
available for earlier versions.

You will need to import the socket module and then call
socket.setdefau lttimeout() to ensure that communication with
non-responsive servers results in a socket exception that you can trap.

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #3

Steve Holden wrote:
wi******@hotmai l.com wrote:
#import urllib, sys
#pages = ['http://www.python.org' , 'http://xxx']
#for i in pages:
# try:
# u = urllib.urlopen( i)
# print u.geturl()
# except Exception, e:
# print >> sys.stderr, '%s: %s' % (e.__class__.__ name__, e)
will print an error if a page fails opening, rest opens fine
More generally you may wish to use the timeout features of TCP

sockets. These were introduced in Python 2.3, though Tim O'Malley's module
"timeoutsoc ket" (which was the inspiration for the 2.3 upgrade) was
available for earlier versions.

You will need to import the socket module and then call
socket.setdefau lttimeout() to ensure that communication with
non-responsive servers results in a socket exception that you can trap.
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119


Thank you wi******@hotmai l.com and Steve for some ideas.Finding the
fact that the script hanged is not a big problem .
I,however, would need a solution that I will not need to start again
the script but the script re-start by itself. I am thinking about two
threads, the main(master) that will supervise a slave thread.This slave
thread will download the pages and whenever there is a timeout the
master thread restart a slave thread.
Is it a good solution? Or is there a better one?
Thanks for help
Lad

Jul 18 '05 #4
Steve Holden wrote:
You will need to import the socket module and then call socket.setdefau lttimeout() to ensure that
communication with non-responsive servers results in a socket exception that you can trap.


or you can use asynchronous sockets, so your program can keep processing
the sites that do respond at once while it's waiting for the ones that don't. for
one way to do that, see "Using HTTP to Download Files" here:

http://effbot.org/zone/effnews-1.htm

(make sure you read the second and third article as well)

</F>

Jul 18 '05 #5

Fredrik Lundh wrote:
Steve Holden wrote:
You will need to import the socket module and then call socket.setdefau lttimeout() to ensure that communication with non-responsive servers results in a socket
exception that you can trap.
or you can use asynchronous sockets, so your program can keep processing the sites that do respond at once while it's waiting for the ones that don't. for one way to do that, see "Using HTTP to Download Files" here:

http://effbot.org/zone/effnews-1.htm

(make sure you read the second and third article as well)

Dear Fredrik Lundh,
Thank you for the link. I checked it. But I have not found an answer to
my question.
My problem is that I can not finish( sometimes) to download all pages.
Sometimes my script freezes and I can not do nothing but restart the
script from the last successfully downloaded web page. There is no
error saying that was an error. I do not know why; maybe the server is
programed to reduce the numbers of connection or there maybe different
reasons.So, my idea was two threads. One master ,suprevising the slave
thread that would do downloading and if the slave thread stopped,
master thread would start another slave. Is it a good solution? Or is
there a better solution?
Thanks for help
Lad

Jul 18 '05 #6

Steve Holden wrote:
wi******@hotmai l.com wrote:
#import urllib, sys
#pages = ['http://www.python.org' , 'http://xxx']
#for i in pages:
# try:
# u = urllib.urlopen( i)
# print u.geturl()
# except Exception, e:
# print >> sys.stderr, '%s: %s' % (e.__class__.__ name__, e)
will print an error if a page fails opening, rest opens fine
More generally you may wish to use the timeout features of TCP

sockets. These were introduced in Python 2.3, though Tim O'Malley's module
"timeoutsoc ket" (which was the inspiration for the 2.3 upgrade) was
available for earlier versions.

You will need to import the socket module and then call
socket.setdefau lttimeout() to ensure that communication with
non-responsive servers results in a socket exception that you can trap.
So adding :

import socket
socket.setdefau lttimeout()
Is *necessary* in order to avoid hangs when using urllib2 to fetch web
resources ?

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml
regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119


Jul 18 '05 #7

Fuzzyman wrote:
Steve Holden wrote:
wi******@hotmai l.com wrote:
#import urllib, sys
#pages = ['http://www.python.org' , 'http://xxx']
#for i in pages:
# try:
# u = urllib.urlopen( i)
# print u.geturl()
# except Exception, e:
# print >> sys.stderr, '%s: %s' % (e.__class__.__ name__, e) will print an error if a page fails opening, rest opens fine
More generally you may wish to use the timeout features of TCP

sockets.
These were introduced in Python 2.3, though Tim O'Malley's module
"timeoutsoc ket" (which was the inspiration for the 2.3 upgrade) was
available for earlier versions.

You will need to import the socket module and then call
socket.setdefau lttimeout() to ensure that communication with
non-responsive servers results in a socket exception that you can

trap.


So adding :

import socket
socket.setdefau lttimeout()
Is *necessary* in order to avoid hangs when using urllib2 to fetch

web resources ?

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml


Fuzzy,
I use HTTPLIB with timeoutsocket but there is no timeout but the script
freezes sometimes.I suspect the server, from which I download pages,
does that to prevent high traffic. I must re start my script.
Do you think Urllib2 would be better?
Or is there a better solution?
Regards,
Lad

Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2517
by: Jerry | last post by:
Hi All, How can I prevent a script from running when a previous instance of the script had already been triggered and the script is running in the background already? So, even when a script is startet several times (eg. by several website visitors) I want only one script-instance running on the server. Subsequent instances should be exited (or prevented) right away to keep the server load as low as possible.
2
3820
by: anonieko | last post by:
Scenario: You have a page that is TOO slow to refresh. But it allows partial flushing of html contents. I.e. Submit button already appears but you don't want your users to click on it prematurely because other parts are still coming. Here I put a javascript the will enable only submit button only after 5 seconds after the page is load fully.
10
3301
by: Thomas Heller | last post by:
I'm about to add some logging calls to a library I have. How can I prevent that the script that uses the library prints 'No handlers could be found for logger "comtypes.client"' when the script runs? I would like to setup the logging so that there is no logging when nothing is configured, and no warning messages are printed. Thomas
3
1593
by: Martin | last post by:
I'm having trouble getting a new PHP/MySQl installation to work. Windows XP Pro, IIS 5.1, PHP 5.1.1, MySQL 5.0.16, ISAPI This is a new computer. The whole setup is for development use only - no access from beyond my own network. PHP seems to be working OK. I can open a test.php page ok - It executes down to the statement where I try to connect to the MySQL database. At that point, nothing further happens. The page is served
5
17451
by: chris.hearson | last post by:
How do I programmatically prevent a service from stopping? I want to be able to keep my service running (started), under certain conditions, when a user tries to stop it. I have tried throwing an exception - from OnStop(): the SCM waits for a long time, reports that the service didn't respond in a timely fashion and then leaves the status as stopping. - from Dispose(): the SCM displays an error dialog with the exception details and...
1
5810
by: Andy | last post by:
My application is written in .NET (C#) with the inline Edit mode from DataGrid. When a SAVE button is pressed it will perform a action on the database either to add, edit or delete data that a user has entered. When the refresh button is pressed at the top of the page the same exact data is re-submitted again resulting duplicate transaction in the database. Is there anyway of stopping this happening? From client-side script, how can we...
0
930
by: Anonieko | last post by:
REM This batch file addresses issues that exist with MS03-32 with REM V1.0 of ASP.NET on Windows XP only REM If you have any other configuration, you should not need to run this @echo off if exist %SystemRoot%\microsoft.net\framework\v1.1.4322\mscorlib.dll goto v11_installed REM "Changing to the Framework install directory"
3
5294
by: GarryJones | last post by:
I found this handy little script on the net that means the user can only press backspace or numbers in form input. <script type="text/javascript"> function numbersonly(e){ var unicode=e.charCode? e.charCode : e.keyCode if (unicode!=8){ //if the key isn't the backspace key (which we should allow) if (unicode<48||unicode>57) //if not a number return false //disable key press
6
14831
Frinavale
by: Frinavale | last post by:
I am trying to ask the user for confirmation before a radio button is selected. Currently I'm handling the radio button's onclick event, asking the user to confirm their action and if they hit the 'cancel' button call the preventDefault() method for the event to cancel it. It cancels the event if a selection has already been made for the radio button group (it actually selects it, the reverts it back to it's original value when the...
0
8853
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8736
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9146
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8025
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2592
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.