Python的urllib2.urlopen()挂起了与Java Restlet服务器的本地连接 [英] Python's urllib2.urlopen() hanging with local connection to a Java Restlet server
问题描述
我正在尝试从python连接到本地运行的Restlet服务器,但连接无限挂起(或者如果我设置超时则超时)。
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
如果我从shell使用 curl
打开上述URL,结果会很快返回。如果我使用urllib2打开一个不同的本地服务(例如端口8000上的Django Web服务器),urllib2工作正常。
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.
我尝试过禁用防火墙(我在OS X上这样做)。我已经尝试将localhost更改为127.0.0.1。除了用户代理之外,来自Restlet的curl和urllib2连接的日志显示相同。
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.
我的解决方法是通过 subprocess
curl >,但我宁愿理解为什么会失败。
My workaround would be to just call curl
via subprocess
, but I'd rather understand why this is failing.
以下是我的Restlet资源的外观:
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
推荐答案
我遇到了类似的问题并最终使用请求包。
I encountered the similar issues and ended up using the Requests package.
这篇关于Python的urllib2.urlopen()挂起了与Java Restlet服务器的本地连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!