Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- py-tar
- ======
- tar file creation
- -----------------
- import tarfile
- tar = tarfile.open("/tmp/bckup.tar","w")
- tar.add("access.log")
- tar.add("logparser.py")
- tar.close()
- Result
- ------
- ! tar -tvf /tmp/bckup.tar
- -rw-rw-r-- sreehari/sreehari 31179568 2023-03-23 22:00 access.log
- -rw-rw-r-- sreehari/sreehari 1002 2023-03-27 20:40 logparser.py
- -----------------------------------------------------------
- import os
- import tarfile
- import posixpath
- directory = "wave_cafe/img/"
- tarName = "/tmp/pics.tar"
- tar = tarfile.open(tarName,"w")
- for item in os.walk(directory):
- curDir = item[0]
- subDirs = item[1]
- subFiles = item[2]
- for subFile in subFiles:
- absPath = posixpath.join(curDir,subFile)
- if absPath.endswith(".png"):
- tar.add(absPath)
- tar.close()
- Result
- ------
- ! tar -tvf /tmp/pics.tar
- -rw-rw-r-- sreehari/sreehari 64185 2020-04-04 11:15 wave_cafe/img/hot-cappuccino.png
- -rw-rw-r-- sreehari/sreehari 58287 2020-04-04 11:15 wave_cafe/img/hot-latte.png
- -rw-rw-r-- sreehari/sreehari 88979 2020-04-07 11:16 wave_cafe/img/about-2.png
- -rw-rw-r-- sreehari/sreehari 46343 2020-04-04 11:15 wave_cafe/img/iced-americano.png
- -rw-rw-r-- sreehari/sreehari 43685 2020-04-04 11:15 wave_cafe/img/smoothie-1.png
- -rw-rw-r-- sreehari/sreehari 85831 2020-04-07 11:16 wave_cafe/img/about-1.png
- -rw-rw-r-- sreehari/sreehari 45394 2020-04-04 11:15 wave_cafe/img/smoothie-4.png
- -rw-rw-r-- sreehari/sreehari 52584 2020-04-04 11:15 wave_cafe/img/iced-espresso.png
- -rw-rw-r-- sreehari/sreehari 50534 2020-04-04 11:15 wave_cafe/img/iced-latte.png
- -rw-rw-r-- sreehari/sreehari 52052 2020-04-04 11:15 wave_cafe/img/iced-cappuccino.png
- -rw-rw-r-- sreehari/sreehari 52159 2020-04-04 11:15 wave_cafe/img/smoothie-3.png
- -rw-rw-r-- sreehari/sreehari 46647 2020-04-04 11:15 wave_cafe/img/smoothie-2.png
- -rw-rw-r-- sreehari/sreehari 61307 2020-04-04 11:15 wave_cafe/img/hot-americano.png
- -rw-rw-r-- sreehari/sreehari 55430 2020-04-04 11:15 wave_cafe/img/hot-espresso.png
- -----------------------------------------------------------
- enabling compression
- ---------------------
- gzip
- ----
- import os
- import tarfile
- import posixpath
- directory = "wave_cafe/img/"
- tarName = "/tmp/pics.tar.gz"
- tar = tarfile.open(tarName,"w:gz")
- for item in os.walk(directory):
- curDir = item[0]
- subDirs = item[1]
- subFiles = item[2]
- for subFile in subFiles:
- absPath = posixpath.join(curDir,subFile)
- if absPath.endswith(".png"):
- tar.add(absPath)
- tar.close()
- Result
- -------
- ! ls -lh /tmp/pics.tar.gz
- -rw-rw-r-- 1 sreehari sreehari 783K Mar 30 17:51 /tmp/pics.tar.gz
- -----------------------------------------------------------
- import os
- import tarfile
- import posixpath
- directory = "wave_cafe/img/"
- tarName = "/tmp/pics.tar.bz2"
- tar = tarfile.open(tarName,"w:bz2")
- for item in os.walk(directory):
- curDir = item[0]
- subDirs = item[1]
- subFiles = item[2]
- for subFile in subFiles:
- absPath = posixpath.join(curDir,subFile)
- if absPath.endswith(".png"):
- tar.add(absPath)
- tar.close()
- Result
- -------
- ! ls -lh /tmp/pics.tar.bz2
- -rw-rw-r-- 1 sreehari sreehari 778K Mar 30 17:53 /tmp/pics.tar.bz2
- -----------------------------------------------------------
- running linux commands
- ----------------------
- import subprocess
- subprocess.call("mkdir /tmp/test",shell=True)
- Result
- -------
- ! ls -d /tmp/test
- /tmp/test
- -----------------------------------------------------------
- reading command line arguments
- ------------------------------
- cat hitcounter.py
- import sys
- print(sys.argv)
- $ python3 hitcounter.py sree hari 94 28
- ['hitcounter.py', 'sree', 'hari', '94', '28']
- -----------------------------------------------------------
- cat hitcounter.py
- import logparser
- import posixpath
- import sys
- arguments = sys.argv[1:]
- use = "python3 ./hitcounter.py /path/to/access.log status-code"
- if len(arguments) == 2:
- logFile = arguments[0]
- statusCode = arguments[1]
- if posixpath.isfile(logFile) and statusCode.isdigit():
- print("ok")
- else:
- print(use)
- else:
- print(use)
- $ python3 hitcounter.py access.log 200
- ok
- $ python3 hitcounter.py access.log 4a4
- python3 ./hitcounter.py /path/to/access.log status-code
- -----------------------------------------------------------
- cat hitcounter.py
- import logparser
- import posixpath
- import sys
- arguments = sys.argv[1:]
- usage = "python3 ./hitcounter.py /path/to/access.log status-code"
- if len(arguments) == 2:
- logFile = arguments[0]
- statusCode = arguments[1]
- if posixpath.isfile(logFile) and statusCode.isdigit():
- logHandler = open(logFile,"r")
- hitCounter = {}
- for logLine in logHandler:
- logDetails = logparser.parser(logLine)
- host = logDetails["host"]
- status = logDetails["status"]
- if status == statusCode:
- if host not in hitCounter:
- hitCounter[host] = 1
- else:
- hitCounter[host] += 1
- def getFreq(t):
- return t[1]
- sortedResult = sorted(hitCounter.items(),key=getFreq)[-10:]
- for item in sortedResult:
- ip = item[0]
- freq = item[1]
- print("{:15} - {}".format(ip,freq))
- else:
- print(usage)
- else:
- print(usage)
- Result
- ------
- $ python3 hitcounter.py access.log 200
- 45.138.4.22 - 830
- 45.153.227.31 - 834
- 45.153.227.55 - 837
- 45.132.207.154 - 845
- 176.222.58.254 - 882
- 45.144.0.179 - 906
- 45.15.143.155 - 1285
- 173.255.176.5 - 2154
- 51.210.183.78 - 2684
- 193.106.31.130 - 89514
- $ python3 hitcounter.py access.log 404
- 90.191.124.115 - 51
- 104.140.103.41 - 57
- 51.210.243.185 - 58
- 104.131.51.209 - 63
- 5.180.107.194 - 66
- 89.159.228.206 - 72
- 193.106.30.100 - 75
- 13.77.204.88 - 78
- 212.9.160.24 - 99
- 173.255.176.5 - 2026
- $ python3 hitcounter.py access.log 301
- 223.238.196.236 - 1
- 52.15.188.132 - 1
- 52.14.77.107 - 1
- 3.22.12.117 - 1
- 109.127.18.197 - 2
- 66.249.66.158 - 2
- 66.249.66.129 - 2
- 193.29.107.244 - 3
- 82.117.212.186 - 3
- 173.255.176.5 - 948
- -----------------------------------------------------------
- cat hitcounter.py
- import logparser
- import posixpath
- import sys
- import ipgeolocation
- arguments = sys.argv[1:]
- usage = "python3 ./hitcounter.py /path/to/access.log status-code"
- if len(arguments) == 2:
- logFile = arguments[0]
- statusCode = arguments[1]
- if posixpath.isfile(logFile) and statusCode.isdigit():
- logHandler = open(logFile,'r')
- hitCounter = {}
- for logLine in logHandler:
- logDetails = logparser.parser(logLine)
- host = logDetails['host']
- status = logDetails['status']
- if status == statusCode:
- if host not in hitCounter:
- hitCounter[host] = 1
- else:
- hitCounter[host] = hitCounter[host] + 1
- def getFreq(t):
- return t[1]
- sortedResult = sorted(hitCounter.items(),key=getFreq)[-10:]
- for item in sortedResult:
- ip = item[0]
- freq = item[1]
- country = ipgeolocation.ip2country(ip=ip,api_key="c9e54a40c52346358b5e1135c973a71b")
- print("{:15} - {:5} [{}]".format(ip,freq,country))
- else:
- print(usage)
- else:
- print(usage)
- Result
- ------
- $ python3 hitcounter.py access.log 200
- 45.138.4.22 - 830 [Germany]
- 45.153.227.31 - 834 [Germany]
- 45.153.227.55 - 837 [Germany]
- 45.132.207.154 - 845 [Germany]
- 176.222.58.254 - 882 [Germany]
- 45.144.0.179 - 906 [Germany]
- 45.15.143.155 - 1285 [United States]
- 173.255.176.5 - 2154 [United States]
- 51.210.183.78 - 2684 [France]
- 193.106.31.130 - 89514 [Ukraine]
- $ python3 hitcounter.py access.log 404
- 90.191.124.115 - 51 [Estonia]
- 104.140.103.41 - 57 [Netherlands]
- 51.210.243.185 - 58 [France]
- 104.131.51.209 - 63 [United States]
- 5.180.107.194 - 66 [Turkey]
- 89.159.228.206 - 72 [France]
- 193.106.30.100 - 75 [Ukraine]
- 13.77.204.88 - 78 [United States]
- 212.9.160.24 - 99 [Germany]
- 173.255.176.5 - 2026 [United States]
- -----------------------------------------------------------
- while loop
- ----------
- x = 1
- while x <= 10:
- print(x)
- x = x + 1
- Result
- -------
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- -----------------------------------------------------------
- l = []
- if l:
- print("not empty")
- else:
- print("empty")
- Result
- -------
- empty
- -----------------------------------------------------------
- l = [2]
- if l:
- print("not empty")
- else:
- print("empty")
- Result
- -------
- not empty
- -----------------------------------------------------------
- if 1:
- print("not empty")
- else:
- print("empty")
- Result
- -------
- not empty
- -----------------------------------------------------------
- if "sree":
- print("not empty")
- else:
- print("empty")
- Result
- -------
- not empty
- -----------------------------------------------------------
- if "":
- print("not empty")
- else:
- print("empty")
- Result
- -------
- empty
- -----------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement