I want to write a script that pings all the ip addresses in a file and displays the status the server name and content length. Now some websites don't display a content length and I want to write if it doesn't exist go on to the next up address.
This is what I have so far
import requests
import os
#input file name
file_name = input("Enter the name of the file: ")
print("hello")
with open(file_name, 'r') as f:
for line in f:
ip = line.strip()
url = "http://" + ip + "/"
print(ip)
response = requests.get(url)
print(response.status_code, len(response.text))
#print(response.text)
#print(response.headers)
#print(response.headers['Content-Type'])
print(response.headers['Server'])
#print(response.headers['X-Powered-By'])
if response.headers['Content-Length'] == None:
print("Missing Content-Length")
else:
print("this is the content type:", response.headers['Content-Length'])
if ip=="":
print("No host supplied")
but I'm still getting an error saying
File "/home/anton/tools/internal_web_tool/web_ip_enumerator.py", line 29, in <module>
if response.headers['Content-Length'] == "":
File "/home/anton/.local/lib/python3.9/site-packages/requests/structures.py", line 54, in __getitem__
return self._store[key.lower()][1]
KeyError: 'content-length'
how can I check if the content length exists or not ?
correction I checked that the content length exists on the ip that I was trying, I but it crashes and gives me the error and I have no idea why