问题
I'm trying to connect to a local running Restlet server from python, but the connection hangs infinitely (or times out if I set a timeout).
import urllib2
handle = urllib2.urlopen("http://localhost:8182/contact/123") # hangs
If I use curl
from a shell to open the above URL, the results return quickly. If I use urllib2 to open a different local service (e.g. a Django web server on port 8000), urllib2 works fine.
I've tried disabling firewall (I'm doing this on OS X). I've tried changing localhost to 127.0.0.1. The logs from Restlet for both the curl and urllib2 connection appear the same aside from the user-agent.
My workaround would be to just call curl
via subprocess
, but I'd rather understand why this is failing.
Here's how my Restlet Resource looks:
public class ContactResource extends ServerResource {
@Get
public String represent() throws Exception {
return "<contact details>";
}
//....
}
Let me know if you want more info/code
回答1:
I encountered the similar issues and ended up using the Requests package.
回答2:
there is ProxyHandler ( http://docs.python.org/library/urllib2.html#urllib2.ProxyHandler ) in urllib2
try to pass empty dictionary to it before urlopen
urllib2.ProxyHandler([])
handle = urllib2.urlopen("http://localhost:8182/contact/123")
来源:https://stackoverflow.com/questions/8332050/pythons-urllib2-urlopen-hanging-with-local-connection-to-a-java-restlet-serve