requestsモジュールを使用して特定のサイトにアクセスして、エラーがあれば通知するPythonプログラムを作成したいです。
Python
1# -*- encoding:utf-8 -*- 2 3import os 4import json 5import traceback 6import requests 7 8try: 9 response = requests.get('http://nourl.example.net') 10 code = response.status_code 11 print(code) 12except requests.exceptions.RequestException as e: 13 print(e)
このソースコードでターゲットURL(ここでいうhttp://nourl.example.net)の
サーバーがダウンしていなければレスポンスコードが帰ってきます。
サーバーがダウンしている場合等接続できないときには例外になるのですが
そのメッセージが
HTTPConnectionPool(host='nourl.example.net', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000000003A7FE80>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))
と長いので「Failed to establish a new connection : getaddrinfo failed」といった感じの文章を取得したいです。
Requestsのドキュメントを見たのですが良くわからなくて。。。よろしくお願いします。
あなたの回答
tips
プレビュー