擅长:python、mysql、java
<p>如果您不需要HEAD方法,只是希望检查主机是否可用,那么为什么不这样做:</p>
<pre><code>from urllib2 import urlopen
try:
u = urlopen("https://eurotableau.nomisonline.com")
u.close()
print "Everything fine!"
except Exception, e:
if hasattr(e, "code"):
print "Server is there but something is wrong with rest of URL"
else: print "Server is on vacations or was never there!"
print e
</code></pre>
<p>这将建立与服务器的连接,但它不会下载任何数据,除非您阅读它。它只读取很少的KB来获取头文件(就像使用HEAD方法时一样),然后等待您请求更多。但你要把它关上。你知道吗</p>
<p>因此,您可以捕获异常并查看问题所在,或者如果没有异常,只需关闭连接。你知道吗</p>
<p>urllib2将处理HTTPS和协议://user@URL:您的端口。
什么都不用担心。你知道吗</p>