问题从json配置文件传递URL到python脚本

时间:2016-10-03 11:12:28

标签: json python-2.7

我目前正在编写一个小型python脚本来监控我的团队Web应用程序池中的所有Url。我有一个python脚本,基本上在无限循环中运行,并将每隔60分钟检查一次网址。我的问题在于从我的json配置中提取我的网址。出于某种原因,我无法使用具有地址的网址:此后的端口和扩展名

我的python脚本或函数如下(部分),当它到达连接部分conn = httplib.HTTPConnection(网站)时基本上就会被炸毁。问题在于以这种格式阅读网址(" url":" zardev0201230265:7778 / apt / server / login /#")

def monitor():
  import httplib
  import logging
  import json
  import os
  p = os.getpid()

#Basic config for / displays what will appear in log file
#Open Json config file and use data function to load and read the file
  logging.basicConfig(filename='SalesTriggerCHECK.log', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
  with open('htppchecklogconfig.json') as json_data_file:
   data = json.load(json_data_file)

#Declare i as counter to iterate through json list, range will be set to length of the list
#Assign data for current iteration for server/category and URL to variables
  for i in range(len(data["application_details"])):
   server = (data["application_details"][i]["server"])
   application = (data["application_details"][i]["application"])
   category = (data["application_details"][i]["category"])
   website = (data["application_details"][i]["url"])

#Connection settings that are required to make a connection request to check URL
#The data function is used in conjunction with the counter(i) to find the URL in the Json file
#Res variable stores response from connection request / if statement checks for status 200 for ok
#We can get the response code and reason using the the get response function when make the connection
   conn = httplib.HTTPConnection(website)
   conn.request("HEAD", "/index.html")
   res = conn.getresponse()

我的JSON配置文件如下:

{
    "application_details": [
          {
            "server": "Server120",
            "application": "sales application",
            "category": "DEV",
            "url": "zardev0201230265:7778/apt/server/login/#"
        },
        {
            "server": "Server130",
            "application": "Dashboard-Hangfire",
            "category": "DEV",
            "url": "zardev0201230297:7779"
        }


    ]

错误详情

Traceback (most recent call last):
  File "C:\Users\pc\Desktop\app\httpchecklog.py", line 50, in <module>
    monitor()
  File "C:\Users\pc\Desktop\app\httpchecklog.py", line 33, in monitor
    conn = httplib.HTTPConnection(website)
  File "C:\Python27\lib\httplib.py", line 751, in __init__
    (self.host, self.port) = self._get_hostport(host, port)
  File "C:\Python27\lib\httplib.py", line 792, in _get_hostport
    raise InvalidURL("nonnumeric port: '%s'" % host[i+1:])
InvalidURL: nonnumeric port: '7778/apt/server/login/#'

任何有关此问题的建议都将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

正如它在异常中所说:非数字端口。 HTTPConnection类将':'后的所有内容解释为端口,在您的情况下:'7778 / apt / server / login /#'。这只能是数字。如果将其更改为“7778”,则不应发生异常。

可以在python文档中找到可用的参数: https://docs.python.org/2/library/httplib.html