fh = open('names.txt', 'r')
for line in fh:
print(line)
for line in fh:
print(line)
fh.seek(0)
for line in fh:
print(line)
fh.close()
fh = open('names.txt')
word_counter = 0
for line in fh:
word = len(line.split())
word_counter += word
fh.close()
word_counter
fh = open('names.txt')
for line in fh:
print(line)
fh.close()
fh = open('names.txt')
for line in fh:
line = line.rstrip()
print(line)
fh.close()
fh = open('syam.txt', 'w')
fh.write("Test string 1")
fh.write("Test string 2")
fh.write("Test string 3")
fh.close()
fuji@fuji:~/python-syam$ cat syam.txt
Test string 1Test string 2Test string 3fuji@fuji:~/python-syam$
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()
fuji@fuji:~/python-syam$ cat syam.txt
Test string 1
test string 2
test string 3
fuji@fuji:~/python-syam$
with open('employee.txt', 'w') as fh:
fh.write("Line 1")
fh.write("\n")
fh.write("Line 2")
fh.write("\n")
print("Program ends.")
with open('wp.conf') as fh:
for line in fh:
line = line.rstrip('\n')
if not line.lstrip().startswith(('/*','*','//')):
if line:
print(line)
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')
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$
log_file = '/home/fuji/Downloads/access_log'
with open(log_file) as fh:
for log_line in fh:
print(log_line)
break
log_file = '/home/fuji/Downloads/access_log'
with open(log_file) as fh:
for log_line in fh:
print(log_line.split())
break
log_file = '/home/fuji/Downloads/access_log'
with open(log_file) as fh:
for log_line in fh:
print(log_line.split()[0])
break
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
logparts
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
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
{'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 }
for ip,hit in ip_counter.items():
if hit > 2000:
print('{:20} : {}'.format(ip,hit))
for ip,hit in ip_counter.items():
if ip not in ('127.0.0.1' , '::1'):
if hit > 2000:
print('{:20} : {}'.format(ip,hit))
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))
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")
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
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)
def myfunc():
result = 2 + 2
return result
myfunc()
x = myfunc()
x
def calc(x,y):
result = x + y
r = calc(2,3)
print(r)
def calc(x,y):
add = x + y
div = x / y
return add,div
calc(5,2)