ping linux服务器的Python脚本

ping linux服务器的Python脚本,python,python-3.x,Python,Python 3.x,我正在寻找一个python脚本,不使用任何外部模块来下载,以检查服务器是否启动。 用户输入将是服务器名称(而不是url) 示例:服务器名称-X123456 最终输出-服务器正在运行您可以使用urllib2和套接字模块执行任务 import socket from urllib2 import urlopen, URLError, HTTPError socket.setdefaulttimeout( 23 ) # timeout in seconds url = 'http://googl

我正在寻找一个python脚本,不使用任何外部模块来下载,以检查服务器是否启动。 用户输入将是服务器名称(而不是url) 示例:服务器名称-X123456
最终输出-服务器正在运行

您可以使用urllib2和套接字模块执行任务

import socket
from urllib2 import urlopen, URLError, HTTPError

socket.setdefaulttimeout( 23 )  # timeout in seconds

url = 'http://google.com/'
try :
    response = urlopen( url )
except HTTPError, e:
    print 'The server couldn\'t fulfill the request. Reason:', str(e.code)
except URLError, e:
    print 'We failed to reach a server. Reason:', str(e.reason)
else :
    html = response.read()
    print 'got response!'
    # do something, turn the light on/off or whatever
可能重复的