Making a persistent HTTP connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Rasmussen

    #1

    Making a persistent HTTP connection

    I use urllib2 to do some simple HTTP communication with a web server. In
    one "session", I do maybe 10-15 requests. It seems that urllib2 opens op
    a connection every time I do a request. Can I somehow make it use _one_
    persistent connection where I can do multiple GET->"receive data" passes
    before the connection is closed?

    /David
  • Diez B. Roggisch

    #2
    Re: Making a persistent HTTP connection

    David Rasmussen wrote:[color=blue]
    > I use urllib2 to do some simple HTTP communication with a web server. In
    > one "session", I do maybe 10-15 requests. It seems that urllib2 opens op
    > a connection every time I do a request. Can I somehow make it use _one_
    > persistent connection where I can do multiple GET->"receive data" passes
    > before the connection is closed?[/color]

    Are you sure HTTP supports that? This would be news to me - which
    doesn't mean much :)

    And even if it works - what is the problem with connections being created?

    Regards,

    Diez

    Comment

    • Benjamin Niemann

      #3
      Re: Making a persistent HTTP connection

      Diez B. Roggisch wrote:
      [color=blue]
      > David Rasmussen wrote:[color=green]
      >> I use urllib2 to do some simple HTTP communication with a web server. In
      >> one "session", I do maybe 10-15 requests. It seems that urllib2 opens op
      >> a connection every time I do a request. Can I somehow make it use _one_
      >> persistent connection where I can do multiple GET->"receive data" passes
      >> before the connection is closed?[/color]
      >
      > Are you sure HTTP supports that? This would be news to me - which
      > doesn't mean much :)[/color]

      It does (HTTP/1.1 at least) and it's called 'keep-alive'.
      [color=blue]
      > And even if it works - what is the problem with connections being created?[/color]

      Performance, network load...

      --
      Benjamin Niemann
      Email: pink at odahoda dot de
      WWW: http://www.odahoda.de/

      Comment

      • Alan Kennedy

        #4
        Re: Making a persistent HTTP connection

        [David Rasmussen][color=blue][color=green]
        >> I use urllib2 to do some simple HTTP communication with a web server.
        >> In one "session", I do maybe 10-15 requests. It seems that urllib2
        >> opens op a connection every time I do a request. Can I somehow make it
        >> use _one_ persistent connection where I can do multiple GET->"receive
        >> data" passes before the connection is closed?[/color][/color]

        [Diez B. Roggisch][color=blue]
        > Are you sure HTTP supports that?[/color]

        Yes, HTTP 1.1 definitely supports multiple requests on the same connection.

        http://www.w3.org/Protocols/rfc2616/...c8.html#sec8.1

        Some HTTP 1.0 clients supported persistent connections through the use
        of the non-standard "keep-alive" header.
        [color=blue]
        > And even if it works - what is the problem with connections being created?[/color]

        The URL above describes the benefits of persistent connections. The
        primary problem of the old style of one-request-per-connection is the
        creation of more sockets than are necessary.

        To the OP: neither urllib nor urllib2 implements persistent connections,
        but httplib does. See the httplib documentation page for an example.



        However, even httplib is "synchronou s", in that it cannot pipeline
        requests: the response to the first request must be competely read
        before a second request can be issued.

        HTH,

        --
        alan kennedy
        ------------------------------------------------------
        email alan: http://xhaus.com/contact/alan

        Comment

        • Alan Kennedy

          #5
          Re: Making a persistent HTTP connection

          [David Rasmussen][color=blue][color=green]
          >> I use urllib2 to do some simple HTTP communication with a web server.
          >> In one "session", I do maybe 10-15 requests. It seems that urllib2
          >> opens op a connection every time I do a request. Can I somehow make it
          >> use _one_ persistent connection where I can do multiple GET->"receive
          >> data" passes before the connection is closed?[/color][/color]

          [Diez B. Roggisch][color=blue]
          > Are you sure HTTP supports that?[/color]

          Yes, HTTP 1.1 definitely supports multiple requests on the same
          connection.

          http://www.w3.org/Protocols/rfc2616/...c8.html#sec8.1

          Some HTTP 1.0 clients supported persistent connections through the use
          of the non-standard "keep-alive" header.
          [color=blue]
          > And even if it works - what is the problem with connections being created?[/color]

          The URL above describes the benefits of persistent connections. The
          primary problem of the old style of one-request-per-connection is the
          creation of more sockets than are necessary.

          To the OP: neither urllib nor urllib2 implements persistent
          connections, but httplib does. See the httplib documentation page for
          an example.



          However, even httplib is "synchronou s", in that it cannot pipeline
          requests: the response to the first request must be competely read
          before a second request can be issued.

          HTH,

          --
          alan kennedy
          ------------------------------------------------------
          email alan: http://xhaus.com/contact/alan

          Comment

          • Piet van Oostrum

            #6
            Re: Making a persistent HTTP connection

            >>>>> "Alan Kennedy" <alanmk@hotmail .com> (AK) wrote:
            [color=blue]
            >AK> http://www.w3.org/Protocols/rfc2616/...c8.html#sec8.1[/color]
            [color=blue]
            >AK> Some HTTP 1.0 clients supported persistent connections through the use
            >AK> of the non-standard "keep-alive" header.[/color]
            [color=blue][color=green][color=darkred]
            >>> And even if it works - what is the problem with connections being created?[/color][/color][/color]
            [color=blue]
            >AK> The URL above describes the benefits of persistent connections. The
            >AK> primary problem of the old style of one-request-per-connection is the
            >AK> creation of more sockets than are necessary.[/color]

            Maybe even more important (and just briefly mentioned in the section
            referred to above) is the latency introduced by the TCP setup and the slow
            startup phase of TCP's congestion control. This calculation is one of the
            exercises the students have to make in my networks class.
            --
            Piet van Oostrum <piet@cs.uu.n l>
            URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
            Private email: piet@vanoostrum .org

            Comment

            • Piet van Oostrum

              #7
              Re: Making a persistent HTTP connection

              >>>>> "Alan Kennedy" <alanmk@hotmail .com> (AK) wrote:
              [color=blue]
              >AK> http://www.w3.org/Protocols/rfc2616/...c8.html#sec8.1[/color]
              [color=blue]
              >AK> Some HTTP 1.0 clients supported persistent connections through the use
              >AK> of the non-standard "keep-alive" header.[/color]
              [color=blue][color=green][color=darkred]
              >>> And even if it works - what is the problem with connections being created?[/color][/color][/color]
              [color=blue]
              >AK> The URL above describes the benefits of persistent connections. The
              >AK> primary problem of the old style of one-request-per-connection is the
              >AK> creation of more sockets than are necessary.[/color]

              Maybe even more important (and just briefly mentioned in the section
              referred to above) is the latency introduced by the TCP setup and the slow
              startup phase of TCP's congestion control. This calculation is one of the
              exercises the students have to make in my networks class.
              --
              Piet van Oostrum <piet@cs.uu.n l>
              URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
              Private email: piet@vanoostrum .org

              Comment

              Working...