Ver código fonte

import

master
Vladimir Smagin 2 anos atrás
commit
adef124d51
2 arquivos alterados com 88 adições e 0 exclusões
  1. +12
    -0
      config-sample.py
  2. +76
    -0
      request_checker.py

+ 12
- 0
config-sample.py Ver arquivo

@@ -0,0 +1,12 @@
searchServer = "http://127.0.0.1:8050"

searchForRequests = {
'analyticpixels.net':
['/pixel.js', '/manager.js']
}

searchURLs = [
'https://google.com/buy/auto/#edit/new/data',
'http://twotowers.ru',
'http://blindage.org/'
]

+ 76
- 0
request_checker.py Ver arquivo

@@ -0,0 +1,76 @@
'''
This program checks urls for specific requests. Requires Splash server https://github.com/scrapinghub/splash
Code by Vladimir Smagin, 2018
Mail: 21h@blindage.org
'''

import requests
import mailer
from urllib.parse import quote
import json
import config

class C:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

def sendmail(subj, body):
message = mailer.Message(From='robot@blindage.org',
To='21h@blindage.org')
message.Subject = subj
message.Html = """<p>Urgent report message<br>
%s""" % body

sender = mailer.Mailer('localhost')
try:
sender.send(message)
except:
print(C.FAIL+"Can't send message to sysadmin!"+C.ENDC)

for pageUrl in config.searchURLs:
try:
print(C.HEADER+"Checking", pageUrl+C.ENDC)

try:
print(requests.get(config.searchServer + "/_ping").text)
print(requests.post(config.searchServer + "/_gc").text)
except:
print(C.FAIL + "Splash server is down. It's time to panic!" + C.ENDC)
break

pageUrlRequest = config.searchServer+"/render.har?url="+ quote(pageUrl) + ""
print(pageUrlRequest)
r = requests.get(pageUrlRequest)
answer = r.json()
if r.status_code is 200:
scriptNotFound = True
for requestUrl in answer['log']['entries']:
url = requestUrl['response']['url']
for uSFR, aSFR in config.searchForRequests.items():

if uSFR in url:
for scriptName in aSFR:
if scriptName in url:
scriptNotFound = False
print(C.OKGREEN + "Found request match", C.OKBLUE + url, uSFR, scriptName + C.ENDC)

if scriptNotFound:
print(C.FAIL + "No matches found" + C.ENDC)
sendmail('URL ' + pageUrl + ' doesn\'t contain needed script request',"<p>URL " + pageUrl +
"</p><p>No matches found with templates</p><p>" + r.text + "</p>")
else:
print(C.WARNING + "Response code is not 200" + C.ENDC)
sendmail('URL ' + pageUrl + ' returned wrong response code', "<p>URL " + pageUrl +
"</p><p>Response code is not 200</p><p>" + r.text + "</p>")

except ValueError:
print(ValueError)
print(C.WARNING + "Couldn't connect to server" + C.ENDC)
sendmail('URL '+ pageUrl+' inacessible', "<p>Status "+str(r.status_code)+"</p><p>Answer<br><br>"+r.text+"</p>")


Carregando…
Cancelar
Salvar