HEX
Server: Microsoft-IIS/10.0
System: Windows NT ITPWINWEBSVR22 10.0 build 20348 (Windows Server 2022) AMD64
User: www.conferencesearch.co.uk (0)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: D:/bin/scripts/python3.4/hmailserver/filetest.py
# (SS,4/4/17)

import time
from datetime import date, timedelta
#import MySQLdb
# use "pip install pymysql" to install pymysql
import pymysql

# Your AWStats LogFormat parameter is: %time2 %email %email_r %host %host_r %method %url %code %bytesd
# This means each line in your web server log file need to have the following personalized log format:
# %time2 %email %email_r %host %host_r %method %url %code %bytesd 
 
# 2017-03-29 10:44:03	sue@sarahharvey.com	clarabertram@aol.com	88.208.239.3	mailin-04.mx.aol.com	SMTP	?	250	5007
 
def check_date(check_date): 
    
  f = open("hmailserver_awstats.log","r")

  count = 0

  for line in f:
    if line[:10] == check_date.isoformat():
      count = count + 1
      add_to_database(line)

  print(count)  
    
  #print(f.readline())

  #print(f.readline())

  f.close()

def clean_sql_str(str):
    return pymysql.escape_string(str)
  
def empty_table():
  sql = 'TRUNCATE mail_log'
  cur.execute(sql) 
  
def get_domain_from_email(email):
  if email == "":
    return ""
  else:
    return email.split("@")[1]
    
def get_host_type(host):
  if host == '88.208.239.3' or host == 'mail.itpartnership.com':
    return "relay"
  elif host == '80.229.252.66' or host == 'mail.ontheworldweb.com':
    return "relay"
  elif host == "127.0.0.1":
    return "local"
  else:
    return "external"
  
def add_to_database(line):
  # split the line
  fields = line.split('\t')

  # for each field in the line:
  
  sql = ""
  f = 0
  domain = ""
  domain_r = ""
  type_in = ""
  type_out = ""
  for field in fields:
    f = f + 1
    if sql != "":
      sql = sql + ", "
    sql = sql + "'" + clean_sql_str(field) + "'"
    if f == 2:
      domain = get_domain_from_email(field)
    elif f == 3:
      domain_r = get_domain_from_email(field)
    elif f == 4:
      type_in = get_host_type(field) 
    elif f == 5:
      type_out = get_host_type(field)          

  sql = sql + ", '" + clean_sql_str(domain) + "'"
  sql = sql + ", '" + clean_sql_str(domain_r) + "'"
  sql = sql + ", '" + clean_sql_str(type_in) + "'"
  sql = sql + ", '" + clean_sql_str(type_out) + "'"
  
  #print(sql)
  sql = 'INSERT INTO mail_log (time, email, email_r, host, host_r, method, url, code, bytesd, domain, domain_r, type_in, type_out) VALUES (' + sql + ')'
  #print(sql)
  #cur.execute(sql.encode('utf-8')) 
  cur.execute(sql) 
  
# charset='utf8' prevents following error because some email addresses before @ may have non-latin1 chars, table may need to be utf8
# UnicodeEncodeError: 'latin-1' codec can't encode character '\u201e' in position 166: ordinal not in range(256)
con = pymysql.connect(host='localhost', user='itpdev', passwd='itp#~4417', db='hmailserver_stats', charset='utf8')
cur = con.cursor(pymysql.cursors.DictCursor)

empty_table()

for i in range(30, 0, -1):
  date_to_check = date.today() - timedelta(days=i)
  print(date_to_check, ' - ', end='')
  check_date(date_to_check)

con.close()