Python's urllib2.urlopen() hanging with local connection to a Java Restlet server

大城市里の小女人 提交于 2019-12-07 00:13:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!