Creating a file handler

In [1]:
fh = open('names.txt', 'r')
In [2]:
for line in fh:
  print(line)
An operating system consists of various fundamental

programs which are needed by your computer so that it

can communicate and receive instructions from users;

read and write data to hard disks, tapes, and printers; 

control the use of memory;

In [3]:
for line in fh:
  print(line)
In [4]:
fh.seek(0)
Out[4]:
0
In [5]:
for line in fh:
  print(line)
An operating system consists of various fundamental

programs which are needed by your computer so that it

can communicate and receive instructions from users;

read and write data to hard disks, tapes, and printers; 

control the use of memory;

In [6]:
fh.close()

Counting total number of words from a file

In [10]:
fh = open('names.txt')
word_counter = 0
for line in fh:
  word = len(line.split())
  word_counter += word
fh.close()
In [9]:
word_counter
Out[9]:
39
In [11]:
fh = open('names.txt')
for line in fh:
  print(line)
fh.close()
An operating system consists of various fundamental

programs which are needed by your computer so that it

can communicate and receive instructions from users;

read and write data to hard disks, tapes, and printers; 

control the use of memory;

Removing new lines while printing from file

In [12]:
fh = open('names.txt')
for line in fh:
  line = line.rstrip()
  print(line)
fh.close()
An operating system consists of various fundamental
programs which are needed by your computer so that it
can communicate and receive instructions from users;
read and write data to hard disks, tapes, and printers;
control the use of memory;

Writing contentds to a file

In [14]:
fh = open('syam.txt', 'w')
fh.write("Test string 1")
fh.write("Test string 2")
fh.write("Test string 3")
fh.close()
In [ ]:
fuji@fuji:~/python-syam$ cat syam.txt 
Test string 1Test string 2Test string 3fuji@fuji:~/python-syam$ 

Fixing new line issue

In [16]:
fh = open('syam.txt' , 'w')
fh.write("Test string 1")
fh.write("\n ")
fh.write("test string 2 ")
fh.write("\n ")
fh.write("test string 3")
fh.write("\n ")

fh.close()
In [ ]:
fuji@fuji:~/python-syam$ cat syam.txt 
Test string 1
 test string 2 
 test string 3
 fuji@fuji:~/python-syam$ 

Creating file handler using context manager

In [17]:
with open('employee.txt', 'w') as fh:
  fh.write("Line 1")
  fh.write("\n")
  fh.write("Line 2")
  fh.write("\n")
print("Program ends.")
Program ends.
In [26]:
with open('wp.conf') as fh:
  for line in fh:
    line = line.rstrip('\n')
    
    if not line.lstrip().startswith(('/*','*','//')):
      if line:
        print(line)
<?php
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );
define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );
$table_prefix = 'wp_';
define( 'WP_DEBUG', false );
if ( ! defined( 'ABSPATH' ) ) {
	define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
require_once( ABSPATH . 'wp-settings.php' );
In [29]:
with open('wp.conf.final', 'w') as fp:
  with open('wp.conf') as fh:
    for line in fh:
      line = line.rstrip('\n')
    
      if not line.lstrip().startswith(('/*','*','//')):
        if line:
          fp.write(line)
          fp.write('\n')
In [ ]:
fuji@fuji:~/python-syam$ cat wp.conf.final 
<?php
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );
define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );
$table_prefix = 'wp_';
define( 'WP_DEBUG', false );
if ( ! defined( 'ABSPATH' ) ) {
	define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
require_once( ABSPATH . 'wp-settings.php' );
fuji@fuji:~/python-syam$ 

counting total number of ip hits

In [30]:
log_file = '/home/fuji/Downloads/access_log'

with open(log_file) as fh:
  for log_line in fh:
    print(log_line)
    break
    
118.24.109.217 - - [30/Mar/2019:00:01:14 +0000] "GET /Drupal.php HTTP/1.1" 404 10086 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)"

In [31]:
log_file = '/home/fuji/Downloads/access_log'

with open(log_file) as fh:
  for log_line in fh:
    print(log_line.split())
    break
['118.24.109.217', '-', '-', '[30/Mar/2019:00:01:14', '+0000]', '"GET', '/Drupal.php', 'HTTP/1.1"', '404', '10086', '"-"', '"Mozilla/4.0', '(compatible;', 'MSIE', '8.0;', 'Windows', 'NT', '5.1;', 'Trident/4.0)"']
In [32]:
log_file = '/home/fuji/Downloads/access_log'

with open(log_file) as fh:
  for log_line in fh:
    print(log_line.split()[0])
    break
118.24.109.217
In [34]:
import logparser

log_file = '/home/fuji/Downloads/access_log'

with open(log_file) as fh:
  for log_line in fh:
    logparts = logparser.parser(log_line)
    print(logparts)
    break
    
{'host': '118.24.109.217', 'identity': '-', 'user': '-', 'time': '30/Mar/2019:00:01:14 +0000', 'request': 'GET /Drupal.php HTTP/1.1', 'status': '404', 'size': '10086', 'referer': '-', 'agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)'}
In [35]:
logparts
Out[35]:
{'host': '118.24.109.217',
 'identity': '-',
 'user': '-',
 'time': '30/Mar/2019:00:01:14 +0000',
 'request': 'GET /Drupal.php HTTP/1.1',
 'status': '404',
 'size': '10086',
 'referer': '-',
 'agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)'}
In [36]:
import logparser

log_file = '/home/fuji/Downloads/access_log'

with open(log_file) as fh:
  for log_line in fh:
    logparts = logparser.parser(log_line)
    print(logparts['host'])
    break
    
118.24.109.217
In [37]:
import logparser

log_file = '/home/fuji/Downloads/access_log'
ip_counter = {}

with open(log_file) as fh:
  for log_line in fh:
    logparts = logparser.parser(log_line)
    ip = logparts['host']
    if ip in ip_counter:
      ip_counter[ip] = ip_counter[ip] + 1
    else:
        ip_counter[ip] = 1
In [41]:
{'118.24.109.217': 115,
 '209.17.96.210': 314,
 '188.213.175.168': 2,
 '209.17.96.26': 273,
 '87.7.228.195': 1 }
Out[41]:
{'118.24.109.217': 115,
 '209.17.96.210': 314,
 '188.213.175.168': 2,
 '209.17.96.26': 273,
 '87.7.228.195': 1}
In [43]:
for ip,hit in ip_counter.items():
  if hit > 2000:
    print('{:20} : {}'.format(ip,hit))
64.62.252.174        : 153147
127.0.0.1            : 122298
::1                  : 157149
46.101.94.163        : 4598
89.187.86.131        : 7072
60.191.38.77         : 19357
64.62.252.163        : 133248
66.160.140.183       : 25800
207.46.13.42         : 2009
125.26.165.189       : 2194
185.234.216.52       : 3545
142.93.74.68         : 4311
165.227.163.166      : 4181
207.46.13.197        : 2989
60.191.84.16         : 2196
41.248.240.252       : 2147
180.180.243.223      : 2259
207.46.13.41         : 2956
79.101.41.129        : 2903
157.55.39.157        : 3100
207.46.13.176        : 4831
In [44]:
for ip,hit in ip_counter.items():
  if ip not in ('127.0.0.1' , '::1'):
    if hit > 2000:
      print('{:20} : {}'.format(ip,hit))
64.62.252.174        : 153147
46.101.94.163        : 4598
89.187.86.131        : 7072
60.191.38.77         : 19357
64.62.252.163        : 133248
66.160.140.183       : 25800
207.46.13.42         : 2009
125.26.165.189       : 2194
185.234.216.52       : 3545
142.93.74.68         : 4311
165.227.163.166      : 4181
207.46.13.197        : 2989
60.191.84.16         : 2196
41.248.240.252       : 2147
180.180.243.223      : 2259
207.46.13.41         : 2956
79.101.41.129        : 2903
157.55.39.157        : 3100
207.46.13.176        : 4831
In [45]:
import logparser

log_file = '/home/fuji/Downloads/access_log'
max_hit = 2000
excludes = ('127.0.0.1' , '::1')


ip_counter = {}

with open(log_file) as fh:
  for log_line in fh:
    logparts = logparser.parser(log_line)
    ip = logparts['host']
    if ip in ip_counter:
      ip_counter[ip] = ip_counter[ip] + 1
    else:
        ip_counter[ip] = 1
        
for ip,hit in ip_counter.items():
  if ip not in excludes:
    if hit > max_hit:
      print('{:20} : {}'.format(ip,hit))
64.62.252.174        : 153147
46.101.94.163        : 4598
89.187.86.131        : 7072
60.191.38.77         : 19357
64.62.252.163        : 133248
66.160.140.183       : 25800
207.46.13.42         : 2009
125.26.165.189       : 2194
185.234.216.52       : 3545
142.93.74.68         : 4311
165.227.163.166      : 4181
207.46.13.197        : 2989
60.191.84.16         : 2196
41.248.240.252       : 2147
180.180.243.223      : 2259
207.46.13.41         : 2956
79.101.41.129        : 2903
157.55.39.157        : 3100
207.46.13.176        : 4831

Reading command line arguement

In [ ]:
fuji@fuji:~/python-syam$ cat command-line-demo.py 
import sys
import os
import logparser

arguements = sys.argv[1:]

if len(arguements) == 2:
  if not os.path.exists(arguements[0]) and not os.path.isfile(arguements[0]):
    print("{} , File not found".format(arguements[0]))
    sys.exit(1)

  if not arguements[1].isdigit():
    print("Entered value {} is not a digit".format(arguements[1]))
    sys.exit(1)

  log_file = arguements[0]
  max_hit = int(arguements[1])
  excludes = ('127.0.0.1' , '::1')


  ip_counter = {}

  with open(log_file) as fh:
    for log_line in fh:
      logparts = logparser.parser(log_line)
      ip = logparts['host']
      if ip in ip_counter:
        ip_counter[ip] = ip_counter[ip] + 1
      else:
        ip_counter[ip] = 1
        
  for ip,hit in ip_counter.items():
    if ip not in excludes:
      if hit > max_hit:
        print('{:20} : {}'.format(ip,hit))
else:
  print("Usage : ./iphitcounter.py  /path/to/access_log  count")
In [ ]:
    fuji@fuji:~/python-syam$ python3 command-line-demo.py /home/fuji/Downloads/access_log 2500
64.62.252.174        : 153147
46.101.94.163        : 4598
89.187.86.131        : 7072
60.191.38.77         : 19357
64.62.252.163        : 133248
66.160.140.183       : 25800
185.234.216.52       : 3545
142.93.74.68         : 4311
165.227.163.166      : 4181
207.46.13.197        : 2989
207.46.13.41         : 2956
79.101.41.129        : 2903
157.55.39.157        : 3100
207.46.13.176        : 4831

    fuji@fuji:~/python-syam$ python3 command-line-demo.py /home/fuji/Downloads/access_log abc
Entered value abc is not a digit
fuji@fuji:~/python-syam$ 
fuji@fuji:~/python-syam$ python3 command-line-demo.py /home/fuji/Downloads/access 2500
/home/fuji/Downloads/access , File not found
fuji@fuji:~/python-syam$ 
fuji@fuji:~/python-syam$ python3 command-line-demo.py /home/fuji/Downloads/access
Usage : ./iphitcounter.py  /path/to/access_log  count
In [ ]:
fuji@fuji:~/python-syam$ cat logparser.py 
#!/usr/bin/env  python3



import re


regex_host = r'(?P<host>.*?)'
regex_identity = r'(?P<identity>\S+)'
regex_user = r'(?P<user>\S+)'
regex_time = r'\[(?P<time>.*?)\]'
regex_request = r'\"(?P<request>.*?)\"'
regex_status = r'(?P<status>\d{3})'
regex_size = r'(?P<size>\S+)'
regex_referer = r'\"(?P<referer>.*?)\"'
regex_agent = r'\"(?P<agent>.*?)\"'
regex_space = r'\s'

pattern = regex_host + regex_space + regex_identity + regex_space + \
          regex_user + regex_space + regex_time + regex_space + \
		  regex_request + regex_space + regex_status + regex_space + \
		  regex_size + regex_space + regex_referer + regex_space + \
		  regex_agent


def parser(s):
	"""
	return type : dict()
	return format: {
                       host:str , identity:str , user:str ,
					   time:str ,request:str , status:str ,
					   size:str , referer:str, agent:str
					}
	returns None if failed.
	"""
	try:
		parts = re.match(pattern,s)
		return parts.groupdict()
	except Exception as err:
		print(err)

Introduction to Functions

In [46]:
def myfunc():
  result = 2 + 2
  return result
In [47]:
myfunc()
Out[47]:
4
In [48]:
x = myfunc()
In [49]:
x
Out[49]:
4
In [50]:
def calc(x,y):
  result = x + y
In [54]:
r = calc(2,3)
In [55]:
print(r)
None
In [56]:
def calc(x,y):
  add = x + y
  div = x / y
  return add,div
 
In [57]:
calc(5,2)
Out[57]:
(7, 2.5)
In [ ]: