Python Mechanize检查服务器是否可用 [英] Python Mechanize to check if a server is available

查看:85
本文介绍了Python Mechanize检查服务器是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,该脚本将读取包含一些url的文件,然后使用机械化模块打开浏览器实例.我只是想知道如果不存在某些URL或服务器无法访问该怎么办.

I'm trying to write a script which will read a file containing some urls and then open a browser instance using mechanize module. I'm just wondering how I can do so if some url does not exist or if the server is unreachable.

例如

import mechanize  

br = mechanize.Browser()  
b  = br.open('http://192.168.1.30/index.php')

我想知道的是,如果192.168.1.30无法访问或http返回404错误,如何从机械化获取信息.

What I want to know is how I will get information from mechanize if 192.168.1.30 is unreachable or if http returns 404 Error.

推荐答案

from mechanize import Browser
browser = Browser()
response = browser.open('http://www.google.com')
print response.code

或使用Python请求库.

Or Use Python requests library.

演示它的示例代码:

>>>import requests
>>> r = requests.get('http://httpbin.org/get')
>>> r.status_code
200

这篇关于Python Mechanize检查服务器是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆