Advertisement
sreehariskumar

py-tar

Mar 30th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.39 KB | None | 0 0
  1. py-tar
  2. ======
  3.  
  4. tar file creation
  5. -----------------
  6. import tarfile
  7. tar = tarfile.open("/tmp/bckup.tar","w")
  8. tar.add("access.log")
  9. tar.add("logparser.py")
  10. tar.close()
  11.  
  12. Result
  13. ------
  14. ! tar -tvf /tmp/bckup.tar
  15.  
  16. -rw-rw-r-- sreehari/sreehari 31179568 2023-03-23 22:00 access.log
  17. -rw-rw-r-- sreehari/sreehari 1002 2023-03-27 20:40 logparser.py
  18.  
  19. -----------------------------------------------------------
  20.  
  21. import os
  22. import tarfile
  23. import posixpath
  24.  
  25. directory = "wave_cafe/img/"
  26. tarName = "/tmp/pics.tar"
  27.  
  28. tar = tarfile.open(tarName,"w")
  29.  
  30. for item in os.walk(directory):
  31.  
  32. curDir = item[0]
  33. subDirs = item[1]
  34. subFiles = item[2]
  35.  
  36. for subFile in subFiles:
  37.  
  38. absPath = posixpath.join(curDir,subFile)
  39.  
  40. if absPath.endswith(".png"):
  41.  
  42. tar.add(absPath)
  43.  
  44. tar.close()
  45.  
  46. Result
  47. ------
  48. ! tar -tvf /tmp/pics.tar
  49.  
  50. -rw-rw-r-- sreehari/sreehari 64185 2020-04-04 11:15 wave_cafe/img/hot-cappuccino.png
  51. -rw-rw-r-- sreehari/sreehari 58287 2020-04-04 11:15 wave_cafe/img/hot-latte.png
  52. -rw-rw-r-- sreehari/sreehari 88979 2020-04-07 11:16 wave_cafe/img/about-2.png
  53. -rw-rw-r-- sreehari/sreehari 46343 2020-04-04 11:15 wave_cafe/img/iced-americano.png
  54. -rw-rw-r-- sreehari/sreehari 43685 2020-04-04 11:15 wave_cafe/img/smoothie-1.png
  55. -rw-rw-r-- sreehari/sreehari 85831 2020-04-07 11:16 wave_cafe/img/about-1.png
  56. -rw-rw-r-- sreehari/sreehari 45394 2020-04-04 11:15 wave_cafe/img/smoothie-4.png
  57. -rw-rw-r-- sreehari/sreehari 52584 2020-04-04 11:15 wave_cafe/img/iced-espresso.png
  58. -rw-rw-r-- sreehari/sreehari 50534 2020-04-04 11:15 wave_cafe/img/iced-latte.png
  59. -rw-rw-r-- sreehari/sreehari 52052 2020-04-04 11:15 wave_cafe/img/iced-cappuccino.png
  60. -rw-rw-r-- sreehari/sreehari 52159 2020-04-04 11:15 wave_cafe/img/smoothie-3.png
  61. -rw-rw-r-- sreehari/sreehari 46647 2020-04-04 11:15 wave_cafe/img/smoothie-2.png
  62. -rw-rw-r-- sreehari/sreehari 61307 2020-04-04 11:15 wave_cafe/img/hot-americano.png
  63. -rw-rw-r-- sreehari/sreehari 55430 2020-04-04 11:15 wave_cafe/img/hot-espresso.png
  64.  
  65. -----------------------------------------------------------
  66.  
  67. enabling compression
  68. ---------------------
  69.  
  70. gzip
  71. ----
  72.  
  73. import os
  74. import tarfile
  75. import posixpath
  76.  
  77. directory = "wave_cafe/img/"
  78. tarName = "/tmp/pics.tar.gz"
  79.  
  80. tar = tarfile.open(tarName,"w:gz")
  81.  
  82. for item in os.walk(directory):
  83.  
  84. curDir = item[0]
  85. subDirs = item[1]
  86. subFiles = item[2]
  87.  
  88. for subFile in subFiles:
  89.  
  90. absPath = posixpath.join(curDir,subFile)
  91.  
  92. if absPath.endswith(".png"):
  93.  
  94. tar.add(absPath)
  95.  
  96. tar.close()
  97.  
  98. Result
  99. -------
  100. ! ls -lh /tmp/pics.tar.gz
  101.  
  102. -rw-rw-r-- 1 sreehari sreehari 783K Mar 30 17:51 /tmp/pics.tar.gz
  103.  
  104. -----------------------------------------------------------
  105.  
  106. import os
  107. import tarfile
  108. import posixpath
  109.  
  110. directory = "wave_cafe/img/"
  111. tarName = "/tmp/pics.tar.bz2"
  112.  
  113. tar = tarfile.open(tarName,"w:bz2")
  114.  
  115. for item in os.walk(directory):
  116.  
  117. curDir = item[0]
  118. subDirs = item[1]
  119. subFiles = item[2]
  120.  
  121. for subFile in subFiles:
  122.  
  123. absPath = posixpath.join(curDir,subFile)
  124.  
  125. if absPath.endswith(".png"):
  126.  
  127. tar.add(absPath)
  128.  
  129. tar.close()
  130.  
  131.  
  132. Result
  133. -------
  134. ! ls -lh /tmp/pics.tar.bz2
  135.  
  136. -rw-rw-r-- 1 sreehari sreehari 778K Mar 30 17:53 /tmp/pics.tar.bz2
  137.  
  138. -----------------------------------------------------------
  139.  
  140.  
  141. running linux commands
  142. ----------------------
  143. import subprocess
  144.  
  145. subprocess.call("mkdir /tmp/test",shell=True)
  146.  
  147. Result
  148. -------
  149. ! ls -d /tmp/test
  150.  
  151. /tmp/test
  152.  
  153. -----------------------------------------------------------
  154.  
  155. reading command line arguments
  156. ------------------------------
  157. cat hitcounter.py
  158.  
  159. import sys
  160. print(sys.argv)
  161.  
  162. $ python3 hitcounter.py sree hari 94 28
  163. ['hitcounter.py', 'sree', 'hari', '94', '28']
  164.  
  165. -----------------------------------------------------------
  166.  
  167. cat hitcounter.py
  168.  
  169. import logparser
  170. import posixpath
  171. import sys
  172.  
  173. arguments = sys.argv[1:]
  174. use = "python3 ./hitcounter.py /path/to/access.log status-code"
  175.  
  176. if len(arguments) == 2:
  177.  
  178. logFile = arguments[0]
  179. statusCode = arguments[1]
  180.  
  181. if posixpath.isfile(logFile) and statusCode.isdigit():
  182.  
  183. print("ok")
  184.  
  185. else:
  186.  
  187. print(use)
  188.  
  189. else:
  190.  
  191. print(use)
  192.  
  193.  
  194. $ python3 hitcounter.py access.log 200
  195. ok
  196.  
  197. $ python3 hitcounter.py access.log 4a4
  198. python3 ./hitcounter.py /path/to/access.log status-code
  199.  
  200. -----------------------------------------------------------
  201.  
  202. cat hitcounter.py
  203.  
  204. import logparser
  205. import posixpath
  206. import sys
  207.  
  208. arguments = sys.argv[1:]
  209. usage = "python3 ./hitcounter.py /path/to/access.log status-code"
  210.  
  211. if len(arguments) == 2:
  212.  
  213.  
  214. logFile = arguments[0]
  215. statusCode = arguments[1]
  216.  
  217. if posixpath.isfile(logFile) and statusCode.isdigit():
  218. logHandler = open(logFile,"r")
  219. hitCounter = {}
  220.  
  221. for logLine in logHandler:
  222. logDetails = logparser.parser(logLine)
  223. host = logDetails["host"]
  224. status = logDetails["status"]
  225.  
  226. if status == statusCode:
  227.  
  228. if host not in hitCounter:
  229.  
  230. hitCounter[host] = 1
  231.  
  232. else:
  233.  
  234. hitCounter[host] += 1
  235.  
  236. def getFreq(t):
  237.  
  238. return t[1]
  239.  
  240. sortedResult = sorted(hitCounter.items(),key=getFreq)[-10:]
  241.  
  242.  
  243. for item in sortedResult:
  244.  
  245. ip = item[0]
  246. freq = item[1]
  247.  
  248. print("{:15} - {}".format(ip,freq))
  249.  
  250. else:
  251.  
  252. print(usage)
  253.  
  254. else:
  255.  
  256. print(usage)
  257.  
  258.  
  259. Result
  260. ------
  261. $ python3 hitcounter.py access.log 200
  262. 45.138.4.22 - 830
  263. 45.153.227.31 - 834
  264. 45.153.227.55 - 837
  265. 45.132.207.154 - 845
  266. 176.222.58.254 - 882
  267. 45.144.0.179 - 906
  268. 45.15.143.155 - 1285
  269. 173.255.176.5 - 2154
  270. 51.210.183.78 - 2684
  271. 193.106.31.130 - 89514
  272.  
  273. $ python3 hitcounter.py access.log 404
  274. 90.191.124.115 - 51
  275. 104.140.103.41 - 57
  276. 51.210.243.185 - 58
  277. 104.131.51.209 - 63
  278. 5.180.107.194 - 66
  279. 89.159.228.206 - 72
  280. 193.106.30.100 - 75
  281. 13.77.204.88 - 78
  282. 212.9.160.24 - 99
  283. 173.255.176.5 - 2026
  284.  
  285. $ python3 hitcounter.py access.log 301
  286. 223.238.196.236 - 1
  287. 52.15.188.132 - 1
  288. 52.14.77.107 - 1
  289. 3.22.12.117 - 1
  290. 109.127.18.197 - 2
  291. 66.249.66.158 - 2
  292. 66.249.66.129 - 2
  293. 193.29.107.244 - 3
  294. 82.117.212.186 - 3
  295. 173.255.176.5 - 948
  296.  
  297. -----------------------------------------------------------
  298.  
  299. cat hitcounter.py
  300.  
  301. import logparser
  302. import posixpath
  303. import sys
  304. import ipgeolocation
  305.  
  306.  
  307. arguments = sys.argv[1:]
  308. usage = "python3 ./hitcounter.py /path/to/access.log status-code"
  309.  
  310. if len(arguments) == 2:
  311. logFile = arguments[0]
  312. statusCode = arguments[1]
  313.  
  314. if posixpath.isfile(logFile) and statusCode.isdigit():
  315. logHandler = open(logFile,'r')
  316. hitCounter = {}
  317.  
  318. for logLine in logHandler:
  319. logDetails = logparser.parser(logLine)
  320. host = logDetails['host']
  321. status = logDetails['status']
  322.  
  323. if status == statusCode:
  324.  
  325. if host not in hitCounter:
  326.  
  327. hitCounter[host] = 1
  328.  
  329. else:
  330.  
  331. hitCounter[host] = hitCounter[host] + 1
  332.  
  333.  
  334. def getFreq(t):
  335. return t[1]
  336. sortedResult = sorted(hitCounter.items(),key=getFreq)[-10:]
  337.  
  338.  
  339. for item in sortedResult:
  340.  
  341. ip = item[0]
  342. freq = item[1]
  343.  
  344. country = ipgeolocation.ip2country(ip=ip,api_key="c9e54a40c52346358b5e1135c973a71b")
  345.  
  346. print("{:15} - {:5} [{}]".format(ip,freq,country))
  347.  
  348. else:
  349.  
  350. print(usage)
  351.  
  352. else:
  353.  
  354. print(usage)
  355.  
  356.  
  357. Result
  358. ------
  359. $ python3 hitcounter.py access.log 200
  360. 45.138.4.22 - 830 [Germany]
  361. 45.153.227.31 - 834 [Germany]
  362. 45.153.227.55 - 837 [Germany]
  363. 45.132.207.154 - 845 [Germany]
  364. 176.222.58.254 - 882 [Germany]
  365. 45.144.0.179 - 906 [Germany]
  366. 45.15.143.155 - 1285 [United States]
  367. 173.255.176.5 - 2154 [United States]
  368. 51.210.183.78 - 2684 [France]
  369. 193.106.31.130 - 89514 [Ukraine]
  370.  
  371. $ python3 hitcounter.py access.log 404
  372. 90.191.124.115 - 51 [Estonia]
  373. 104.140.103.41 - 57 [Netherlands]
  374. 51.210.243.185 - 58 [France]
  375. 104.131.51.209 - 63 [United States]
  376. 5.180.107.194 - 66 [Turkey]
  377. 89.159.228.206 - 72 [France]
  378. 193.106.30.100 - 75 [Ukraine]
  379. 13.77.204.88 - 78 [United States]
  380. 212.9.160.24 - 99 [Germany]
  381. 173.255.176.5 - 2026 [United States]
  382.  
  383. -----------------------------------------------------------
  384.  
  385.  
  386. while loop
  387. ----------
  388. x = 1
  389. while x <= 10:
  390. print(x)
  391. x = x + 1
  392.  
  393. Result
  394. -------
  395. 1
  396. 2
  397. 3
  398. 4
  399. 5
  400. 6
  401. 7
  402. 8
  403. 9
  404. 10
  405.  
  406. -----------------------------------------------------------
  407.  
  408. l = []
  409.  
  410. if l:
  411.  
  412. print("not empty")
  413.  
  414. else:
  415.  
  416. print("empty")
  417.  
  418. Result
  419. -------
  420. empty
  421.  
  422. -----------------------------------------------------------
  423.  
  424. l = [2]
  425.  
  426. if l:
  427.  
  428. print("not empty")
  429.  
  430. else:
  431.  
  432. print("empty")
  433.  
  434. Result
  435. -------
  436. not empty
  437.  
  438. -----------------------------------------------------------
  439.  
  440. if 1:
  441.  
  442. print("not empty")
  443.  
  444. else:
  445.  
  446. print("empty")
  447.  
  448. Result
  449. -------
  450. not empty
  451.  
  452. -----------------------------------------------------------
  453.  
  454. if "sree":
  455.  
  456. print("not empty")
  457.  
  458. else:
  459.  
  460. print("empty")
  461.  
  462. Result
  463. -------
  464. not empty
  465.  
  466. -----------------------------------------------------------
  467.  
  468. if "":
  469.  
  470. print("not empty")
  471.  
  472. else:
  473.  
  474. print("empty")
  475.  
  476. Result
  477. -------
  478. empty
  479.  
  480. -----------------------------------------------------------
  481.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement