如何计算列表中的每一项?

2024-10-04 01:31:28 发布

您现在位置:Python中文网/ 问答频道 /正文

上下文:

我有一个程序,可以解析我的盒子上的连接尝试的日志文件。 它提取一个ip地址列表,然后我有其他的功能,我想在这个信息列表上运行,特别是其中一个没有按预期工作。它应该得到每个国家的连接尝试次数。在

代码-ip_工具.py在

#!/usr/bin/python

import requests
import json
import socket

#function to get the ip address of the host user
def get_host_ipad():
    host_ip_request = requests.get("http://ipinfo.io/ip")
    return host_ip_request.text

#function to get gelocation info of a remote host
def get_rhost_geo(ipad):
    full_geo_info = {}
    rhost_ip_request = requests.get("http://ipinfo.io/%s/json" % (ipad))
    json_response = json.loads(rhost_ip_request.text)
    for value in json_response:
        full_geo_info.update({str(value) : str(json_response[value])})
    return full_geo_info

#function to return country of rhost
def get_geo_country(ipad):
    geo_info = get_rhost_geo(ipad)
    geo_country = geo_info["country"]
    return geo_country

#function to perform reverse dns lookup
def get_rhost_url(ipad):
    try:
        rhost_url = socket.gethostbyaddr(ipad)
        rhost_url = rhost_url[0]
    except Exception:
        rhost_url = 'No URL found for this ip address.'
    return rhost_url



#main function to run the code only if called directly
def Main():

#printing the options menu and taking a variable to be passed
    print '_' * 20
    print "1: Get your ip address: \n"
    print "2: Get Geolocation of an IP address: \n"
    print "3: Atempt getting URL from IP address"
    sel = raw_input("Choose an option: \n")

#if statement to control menu navigation
    if sel == '1':
        print get_host_ipad()

#calls the get_rhost_ipad function defined above on user input
    elif sel == '2':
        ipad = raw_input("Which IP address?: \n")
        print get_rhost_geo(ipad)
    elif sel == 'quit':
        quit()
    elif sel == '3':
        ipad = raw_input("Which IP address?: \n")
        print get_rhost_url(ipad)

    else:
        print "Please choose one of the other options. \n"

if __name__=="__main__":
        Main()

代码-日志_审核员.py公司名称:

^{pr2}$

所讨论的功能是:

#function to get attempts per country
def get_country_count():
    ip_list = get_ips_from_log()
    get_country_count = [(ip_tools.get_geo_country(ip), ip_list.count(ip)) for ip in set(ip_list)]
    return get_country_count

不幸的是,它的输出看起来像:

[('CN', 2), ('CN', 566), ('NL', 2), ('CN', 3040), ('CN', 2), ('CN', 1042), ('CN', 2), ('US', 2), ('KR', 382), ('DE', 2), ('US', 127)]

我怎样才能让这个组合更进一步呢?在


Tags: thetoipjsonhosturlgetaddress
2条回答

使用字典记录每个国家代码的总计数。在

from collections import defaultdict
def get_country_count():
    ip_list = get_ips_from_log()
    country_count = defaultdict(int)
    for ip in set(ip_list):
        country_count[ip_tools.get_geo_country(ip)] += ip_list.count(ip)
    return country_count

如果我们不接触所有其他代码,函数可以这样重写:

from collections import defaultdict

....

def get_country_count():
    country_count = defaultdict(int)
    for ip in get_ips_from_log():
        country_count[ip_tools.get_geo_country(ip)] += 1
    return country_count

或者如果使用get_geo_country花费很大:

^{pr2}$

defaultdict仅用于某个原因,不要编写这样难看的构造:

def get_country_count():
   ....
   for ....
       country = ip_tools.get_geo_country(ip)
       if country in country_count:
           country_count[country] += ...
       else:
           country_count[country] = ...

相关问题 更多 >