urllib2 problem/bug: Request.add_header does() nothing?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kelio@yandex.ru

    urllib2 problem/bug: Request.add_header does() nothing?

    I have a simple cgi-script on a server that prints all key-value pairs
    from a request. And it really works when i use a browser and type smth
    like http://server/cgi-bin/test?name=mike&johny=dummy. But when I use
    the following script, nothing is printed (like i type
    http://server/cgi-bin/test request in the browser).

    What is wrong about it? It's hard to believe there's a bug nobody's
    noticed for such a long time (I've tried Python 2.1.3 and 2.4.1 on
    Windows, and 2.2.2 on Linux).

    I've also tried to make a request using urllib (without 2) sending
    pairs as data in a POST request - and it also worked.

    Thanks for help!

    elio.

    #!/usr/bin/python

    import urllib2

    request = urllib2.Request (r"http://server/cgi-bin/test")
    request.add_hea der("product", "SohoCore")
    request.add_hea der("version", "1.1")
    request.add_hea der("build", "1251")

    reply = urllib2.urlopen (request).readl ines()

    for i in reply:
    print i

  • Fuzzyman

    #2
    Re: urllib2 problem/bug: Request.add_hea der does() nothing?



    kelio@yandex.ru wrote:[color=blue]
    > I have a simple cgi-script on a server that prints all key-value pairs
    > from a request. And it really works when i use a browser and type smth
    > like http://server/cgi-bin/test?name=mike&johny=dummy. But when I use
    > the following script, nothing is printed (like i type
    > http://server/cgi-bin/test request in the browser).
    >
    > What is wrong about it? It's hard to believe there's a bug nobody's
    > noticed for such a long time (I've tried Python 2.1.3 and 2.4.1 on
    > Windows, and 2.2.2 on Linux).
    >
    > I've also tried to make a request using urllib (without 2) sending
    > pairs as data in a POST request - and it also worked.
    >
    > Thanks for help!
    >
    > elio.
    >
    > #!/usr/bin/python
    >
    > import urllib2
    >
    > request = urllib2.Request (r"http://server/cgi-bin/test")
    > request.add_hea der("product", "SohoCore")
    > request.add_hea der("version", "1.1")
    > request.add_hea der("build", "1251")
    >
    > reply = urllib2.urlopen (request).readl ines()
    >
    > for i in reply:
    > print i[/color]


    The add_header method adds http headers to your http request - it
    *doesn't* post any data to your CGI.

    What you need to do is encode a dictionary of your parameters using
    ``urllib.urlenc ode`` (*not* urllib2) - then pass it as the second
    parameter to urllib2.urlopen .

    HTH

    Best Regards,

    Fuzzy


    Comment

    Working...