Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:42:43+00:00 2026-06-02T23:42:43+00:00

Overview I am using urlopen from the Python 2.7.1 urllib2 package to do a

  • 0

Overview

I am using urlopen from the Python 2.7.1 urllib2 package to do a HTTP POST form a Windows XP machine to a remote Apache webserver (for instance the built-in web sharing of Mac OS X). The sent data contains some identifier, data and a checksum, if all data is sent the server responds with an acknowledgement. The checksum in the data can be used to check if everything arrived in fine order.

The Problem

Usually this works great, however sometimes the internet connection is bad, often because the client sending the data uses a wifi or 3G connection. This results in internet connection loss for some arbitrary amount time. urlopen contains a timeout option, to make sure that this does not block your program and it can continue.

This is what I want, but the problem is that urlopen does not stop the socket from continuing to send whatever data it still had to send when the timeout occurred. I have tested this (with the code that I will show below) by trying to send a large bit of data to my laptop, I would see the network activity on both show activity, I’d then stop the wireless on the laptop, wait until the function times out, and then reactivate the wireless, and the data transfer would then continue, but the program will not be listening for responses anymore. I even tried to exit the Python interpreter and it would still send data, so control of that is handed over to Windows somehow.

Causes

The timeout (as I understand it) works like this:
It checks for an ‘idle response time’
( [Python-Dev] Adding socket timeout to urllib2 )
If you set the timeout to 3, it will open the connection, start a counter, then try to send the data and wait for a response, if at any point before receiving the response the timer runs out a timeout exception is called. Note that the sending of the data does not seem to count as ‘activity’ a far as the timeout timer is concerned.
( urllib2 times out but doesn't close socket connection )
( Close urllib2 connection )

Apparently it is somewhere stated that when a socket is closed/dereferenced/garbage collected it calls its ‘close’ function which waits for all data to be sent before closing the socket. However there is also a shutdown function, which should stop the socket immediately, preventing any more data to be sent.
( socket.shutdown vs socket.close )
( http://docs.python.org/library/socket.html#socket.socket.close )

What I Want

I want the connection to be ‘shutdown’ when a timeout occurs. Otherwise my client will not be able to tell if the data was received properly or not and it might try to send it again. I’d rather just kill the connection and try again later, knowing that the data was (probably) not send successfully (the server can recognize this if the checksum does not match).

Here is part of the code that I used to test this. The try..except parts do not yet work as I’d expect, any help there is also appreciated. As I said before I want the program to shutdown the socket as soon as the timeout (or any other) exception is raised.

from urllib import urlencode
from urllib2 import urlopen, HTTPError, URLError
import socket
import sys

class Uploader:
    def __init__(self):
        self.URL = "http://.../"
        self.data = urlencode({'fakerange':range(0,2000000,1)})
        print "Data Generated"

    def upload(self):
        try:
            f = urlopen(self.URL, self.data, timeout=10)
            returncode = f.read()
        except (URLError, HTTPError), msg:
            returncode = str(msg)
        except socket.error:
            returncode = "Socket Timeout!"
        else:
            returncode = 'Im here'

def main():
    upobj = Uploader()
    returncode = upobj.upload()

    if returncode == '100':
        print "Success!"
    else:
        print "Maybe a Fail"
        print returncode
    print "The End"

if __name__ == '__main__':
main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:42:44+00:00Added an answer on June 2, 2026 at 11:42 pm

    It turns out that calling the .sock.shutdown(socket.SHUT_RDWR) and .close() commands on a HTTPConnection that is uploading does not stop the upload. It will continue running in the background. I am not aware of more reliable/direct methods to kill the connection from Python, while using urllib2 or httplib.
    In the end we tested the upload using urllib2 without the timeout. This means that on a slow connection it might take very long to do the upload (POST), but at least we will know wether it worked or not. There is a possibility that urlopen might hang because there is no timeout, but we have tested various bad-connection possibilities and in all cases the urlopen either worked or returned an error after some time.
    This means that we will at least know, in the client side, that the upload succeeded or failed, and that it does not continue in the background.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jQuery star rating control found at http://www.fyneworks.com/jquery/star-rating/#tab-Overview . I am using
Overview: I'm working on some windows service using Visual Basic 2010, which deployed on
I am using this plugin. http://jquery.malsup.com/block/#overview However I would like this blockUI to only
Overview From the GR32 library, I am using TImgView32 to render a grid which
I am working on web application and using GAE/J blobstore tutorial http://code.google.com/appengine/docs/java/blobstore/overview.html I was
I'm using URL Fetch Java API http://code.google.com/appengine/docs/java/urlfetch/overview.html#Fet ... to get information from graph.facebook.com, however,
I am using this library to hold information about tree structure: http://www.datasoftsolutions.net/tree_container_library/overview.php Here is
Overview I am using CompositeWPF to create an app using C#. This really should
Overview I am trying to get a photo feed on to my site using
I'm using the Drupal views module to create an overview of nodes. In the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.