chrisluiz16

Untitled

Jul 28th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. def hello():
  2. print('Hello world')
  3. hello()
  4. hello()
  5. hello()
  6.  
  7. ##
  8.  
  9. def calc():
  10. x = 5
  11. y = 3
  12. total = x+y
  13. print(total)
  14. calc()
  15. calc()
  16. calc()
  17.  
  18. ## Argument
  19.  
  20. def calc(first,second):
  21. x = first
  22. y = second
  23. total = x + y
  24. print(total)
  25. calc(5,3)
  26. calc(10,15)
  27. calc(9,8)
  28.  
  29. ##
  30.  
  31. def calc(first,second):
  32. x = first
  33. y = second
  34. total = x + y
  35. print(total)
  36. result = calc(5,3)
  37. print(result)
  38.  
  39. ## Return
  40.  
  41. def calc(first,second):
  42. x = first
  43. y = second
  44. return x + y
  45.  
  46. result = calc(10,10)
  47. print(result)
  48.  
  49. ##
  50.  
  51. def mylen(iterable):
  52. l = 0
  53. for item in iterable:
  54. l += 1
  55. return l
  56. print(mylen([1,2,3]))
  57.  
  58. ##
  59.  
  60. print(mylen('prince'))
  61.  
  62. ## Multiple return
  63.  
  64. def mylen(iterable):
  65. l = 0
  66. for item in iterable:
  67. l += 1
  68. return 'Hello'
  69. return l
  70.  
  71. print(mylen('prince'))
  72.  
  73. ##
  74.  
  75. def mycal(x,y):
  76. r1 = x + y
  77. r2 = x * y
  78. return [r1,r2]
  79.  
  80.  
  81. result = mycal(10,10)
  82. print(result)
  83.  
  84. ## Default argument
  85.  
  86. def calc(x,y):
  87. return x + y
  88.  
  89. r = calc(5,3)
  90. print(r)
  91.  
  92. def calc(x=0,y=0):
  93. return x + y
  94.  
  95. r = calc(5)
  96. print(r)
  97.  
  98. r = calc(5,4)
  99. print(r)
  100.  
  101. ## SSh
  102.  
  103. def sshconfig(user,port,host):
  104. return "ssh -p {} {}@{}".format(port,user,host)
  105.  
  106. r = sshconfig("ec2-user",22,"3.3.3.3")
  107. print(r)
  108.  
  109. ##
  110.  
  111. def sshconfig(host,port=22,user='root'):
  112. return "ssh -p {} {}@{}".format(port,user,host)
  113.  
  114. r = sshconfig('1.1.1.1')
  115. print(r)
  116.  
  117. r = sshconfig('1.1.1.1',2020)
  118. print(r)
  119.  
  120. r = sshconfig('1.1.1.1',2020,'ec2-user')
  121. print(r)
  122.  
  123. r = sshconfig('1.1.1.1','alex')
  124. print(r)
  125.  
  126. ## Keyword argument
  127.  
  128. def sshconfig(host,port=22,user='root'):
  129. return "ssh -p {} {}@{}".format(port,user,host)
  130.  
  131. r = sshconfig(user = 'sam',port= '2323',host = '109.145.23.99')
  132. print(r)
  133.  
  134. ## Requests
  135.  
  136. import requests
  137.  
  138. ip_url = "https://api.ipgeolocation.io/ipgeo?apiKey=31fcd226b4674a85ac09fe288740ef9b&ip=8.8.8.8"
  139.  
  140. response = requests.get(ip_url)
  141.  
  142. print(response.headers)
  143. print()
  144.  
  145. print(response.status_code)
  146. print()
  147.  
  148. print(response.text)
  149.  
  150.  
  151. ## Json
  152.  
  153. import requests
  154.  
  155. ip_url = "https://api.ipgeolocation.io/ipgeo?apiKey=31fcd226b4674a85ac09fe288740ef9b&ip=8.8.8.8"
  156.  
  157. response = requests.get(url = ip_url)
  158.  
  159. json_data = response.json()
  160. print(json_data)
  161.  
  162. print(json_data['ip'])
  163.  
  164. import requests
  165.  
  166.  
  167. def get_country(ip = None):
  168. if ip != None:
  169.  
  170. ip_url = "https://api.ipgeolocation.io/ipgeo?apiKey=31fcd226b4674a85ac09fe288740ef9b&ip={}".format(ip)
  171.  
  172. response = requests.get(url = ip_url)
  173.  
  174. json_data = response.json()
  175. return json_data['country_name']
  176. else:
  177. return None
  178.  
  179. r = get_country('3.3.3.3')
  180. print(r)
  181.  
  182. r = get_country('9.9.9.9')
  183. print(r)
  184.  
  185. ## Module
  186.  
  187. import ipcountry
  188.  
  189. import logparser
  190. access_log = "access.log"
  191. logfile = open(access_log,"r")
  192. ipcounter = {}
  193.  
  194. for line in logfile:
  195. logParts = logparser.parser(line)
  196. host = logParts['host']
  197. status = logParts['status']
  198. if status == "404":
  199. if host not in ipcounter:
  200. ipcounter[host] = 1
  201. else:
  202. ipcounter[host] = ipcounter[host] + 1
  203.  
  204. for ip in ipcounter:
  205. freq = ipcounter[ip]
  206. if freq >= 50:
  207. country = ipcountry.get_country(ip = ip)
  208. out = "{:20} - {:6} - {} ".format(ip,freq,country)
  209. print(out)
  210.  
  211. ## iseven
  212.  
  213. def iseven(x):
  214. if x % 2 == 0:
  215. return True
  216. else:
  217. return False
  218.  
  219. ##
  220. def isodd(x):
  221. if x % 2 != 0:
  222. return True
  223. else:
  224. return False
  225.  
  226. number = iseven(8)
  227. print(number)
  228.  
  229. number = iseven(9)
  230. print(number)
  231.  
  232. number = isodd(9)
  233. print(number)
  234.  
  235.  
Advertisement
Add Comment
Please, Sign In to add comment