473,804 Members | 3,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

urllib2 problem/bug: Request.add_hea der 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

Jul 25 '05 #1
1 3230


ke***@yandex.ru wrote:
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

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
http://www.voidspace.org.uk/python

Jul 27 '05 #2

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

Similar topics

4
4463
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using python 2.3 Here is an exemple of the piece of code I use: import urllib2 #Proxy handler proxy_handler = urllib2.ProxyHandler({"http" :
1
3966
by: Matthew Wilson | last post by:
I am writing a script to check on my router's external IP address. My ISP refreshes my IP very often and I use dyndns for the hostname for my computer. My Netgear mr814 router has a webserver that uses HTTP basic authorization. My script uses urllib2 to connect to the router and read the html page with the current external IP address. This is the function I wrote to lookup the router's external IP: def mr814(router_user,...
0
1009
by: Stephen Waterbury | last post by:
I'm creating an HTTP client file-upload function and noticed that there seems to be no nice way of using urllib2.Request and email.MIMEMultipart together, due to their inconsistent representations of RFC 2822 headers. My naive hope was to inherit from both: class UploadRequest(urllib2.Request, email.MIMEMultipart)
0
1785
by: Richie Hindle | last post by:
Hi, I'm trying to write a test script to hammer an HTTP server over a persistent HTTP 1.1 connection. The server uses cookies, so I'm using a combination of ClientCookie 0.4.19 and urllib2 with Python 2.3.4 on Windows XP. This is what I have so far: import urllib2, ClientCookie UA = 'Mozilla/4.0 (compatible; MSIE 6.0;)' URL = 'http://server/resource/'
0
2659
by: jacob c. | last post by:
When I request a URL using urllib2, it appears that urllib2 always makes the request using HTTP 1.0, and not HTTP 1.1. I'm trying to use the "If-None-Match"/"ETag" HTTP headers to conserve bandwidth, but if I'm not mistaken, these are HTTP 1.1 headers, so I can't reasonably expect a web server to respond correctly to my requests. (In my limited testing, it looks like some servers respond correctly with an HTTP 304 status, while some...
3
2023
by: EP | last post by:
Hello patient and tolerant Pythonistas, Iterating through a long list of arbitrary (and possibly syntactically flawed) urls with a urllib2 pinging function I get a hang up. No exception is raised, however (according to Windows Task Manager) python.exe stops using any CPU time, neither increasing nor decreasing the memory it uses, and thescript does not progress (permanently stalled, it seems). As an example, the below function has been...
3
10212
by: m.banaouas | last post by:
Hi all, I started to use urllib2 library and HTTPBasicAuthHandler class in order to authenticate with a http server (Zope in this case). I don't know why but it doesn't work, while authenticating with direct headers manipulation works fine! WinXP Sp2 Python 2.4.4 Thanks in advance for your help/comments
5
1752
by: Adrian Smith | last post by:
I'm trying to use urllib2 to download a page (I'd rather use urllib, but I need to change the User-Agent header to look like a browser or G**gle won't send it to me, the big meanies). The following (pinched from Dive Into Python) seems to work perfectly in Idle, but falls at the final hurdle when run as a cgi script - can anyone suggest anything I may have overlooked? request = urllib2.Request(some_URL) request.add_header('User-Agent',...
1
2336
by: tepples | last post by:
The HTTP specification, RFC 2616, states that field names in HTTP headers are case insensitive. But I'm trying to use Python 2.5.1's urllib2 to interact with a web service offered by Amazon.com, which deviates from the HTTP spec in that it requires use of case-sensitive header names ReportName, ReportID, and NumberOfReports. I try to send an HTTP header named "NumberOfReports", but it comes out mangled as "Numberofreports'. What is the...
0
9706
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
9579
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
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10320
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
10077
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
9150
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
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2991
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.