views:

67

answers:

2

My code fetches CSV data from a PHP page using httplib. When I open the page in Firefox or Chrome, the data displays just fine. However, when I try to fetch it with my python code, I get a header with content-length: 0 and no data. This page is the only one that does this - in another page in the same directory, the python httplib fetching works just fine. Can someone tell me what I'm doing wrong?

code:

FILE_LOC = '/core/csv.php'
argstr = '?type=' + self.type + '&id=' + self.id
conn = httplib.HTTPConnection(SERVER_ADDRESS)
conn.request('GET', FILE_LOC + argstr)
resp = conn.getresponse()
csvstr = resp.read()

The response headers:

[('content-length', '0'), ('x-powered-by', 'PHP/5.1.6'),
('server', 'Apache/2.2.3 (CentOS)'), ('connection', 'close'),
('date', 'Thu, 19 Aug 2010 21:39:44 GMT'), ('content-type', 'text/html; charset=UTF-8')]
+1  A: 

Perhaps the PHP script expects to see some HTTP header or headers that the httplib module isn't sending. For example, httplib does not seem to send Accept, Accept-Language, or User-Agent headers by default. You may need to add one or more of those to the request() call. It does seem to send a proper Host header, though, which was my first guess.

kindall
my guess would be that the `Accept` header is key here but OP doesn't specify if other urls for which `urllib` work are CSV or not. `text/csv` might be a good one to try.
aaronasterling
+1  A: 

Probably filtering on User-Agent header -- try spoofing e.g. your Firefox.

Failing that you could use Firefox to connect to a local Python server to see exactly what headers it is sending, and then replicate those.

katrielalex
i'm not the -1 but you can just use firebug to see what headers firefox is sending ;) Also, aside from your use of 'probably' instead of 'possibly', I'm not sure why this got downvoted. There are other things to check first.
aaronasterling
heh, I didn't know that! Useful.
katrielalex