问题描述:

My python script needs to establish two HTTPConnection from the httplib library, each connecting to a different domain to GET data. I also have to make multiple requests to those domains multiple times. The data returned from requests is all in JSON.

import httplib, json

... ...

# Establish connection to DOMAIN1

conn1 = httplib.HTTPConnection("DOMAIN1")

conn1.request("GET", "URL1")

objs1 = json.loads(conn1.getresponse().read())

print json.dumps(objs1, indent=4)

#-- here, objs1 is nicely printed out in python console --#

#Establish connection to DOMAIN2

conn2 = httplib.HTTPConnection("DOMAIN2")

conn2.request("GET", "URL2")

objs2 = json.loads(conn2.getresponse().read())

print json.dumps(objs2 , indent=4)

#-- here, objs1 is nicely printed out in python console --#

# Reuse the first connection to DOMAIN1 and call the same URL

conn1.request("GET", "URL1")

objs3 = json.loads(conn1.getresponse().read()) #-- here, socket.error is thrown out! The program does not go any further --#

print json.dumps(objs3, indent=4)

... ...

I suspect it's due to multiple connections to different domains as even I removed the connection to DOMAIN2 and put in a few more connections to DOMAIN1 for testing purpose, my code worked!

Any idea to solve the problem?

相关阅读:
Top