首页 > 解决方案 > Comparing Network Traffic to Authorized List(s) via Domain Name

问题描述

I'm trying to parse through network traffic and compare the domain names in the traffic to a list of the most common websites. the intent is to print all the site names that are not on the list of common websites


with open('/Users/downloads/scripting_for_security/resources/top_100.txt') as f:
    safeAdd = f.readlines(),


with open('/Users/downloads/scripting_for_security/resources/traffic_log.txt') as n:
    netTraffic = n.readlines(),

domainTraffic = re.findall(r'\s(?:www.)?(\w+.com)', netTraffic)


for i in safeAdd:
    for e in domainTraffic:
        if i != e:
            print(e)

getting a type error

TypeError Traceback (most recent call last) in 8 netTraffic = n.readlines(), 9 ---> 10 domainTraffic = re.findall(r'\s(?:www.)?(\w+.com)', netTraffic) 11 12

~/anaconda3/lib/python3.7/re.py in findall(pattern, string, flags) 221 222 Empty matches are included in the result.""" --> 223 return _compile(pattern, flags).findall(string) 224 225 def finditer(pattern, string, flags=0):

TypeError: expected string or bytes-like object

标签: pythonregex

解决方案


netTraffic is a list as per https://docs.python.org/3/tutorial/inputoutput.html

findall expects a second argument of type string https://docs.python.org/3/library/re.html#re.findall


推荐阅读