Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ipcountry
- host = input("Enter the ipaddress: ")
- result = ipcountry.get_country(ip = host)
- print(result)
- ## Reading command line arguments
- ! python3 ipsearch.py 2.2.2.2
- ! cat ipsearch.py
- import ipcountry
- import sys
- host = sys.argv[1]
- result = ipcountry.get_country(ip = host)
- print(result)
- import ipcountry
- import sys
- host = sys.argv
- if len(host) == 2:
- arg = sys.argv[1]
- result = ipcountry.get_country(ip = arg)
- print(result)
- else:
- print('Please provide an Ip address')
- ! python3 new.py 9.9.9.9
- ## urls
- import requests
- url = [
- 'https://www.google.com',
- 'https://www.youtube.com',
- 'https://www.fsf.org',
- 'https://www.stackoverflow.com',
- 'https://www.gnu.org',
- 'https://www.kernel.org'
- ]
- for hostname in url:
- response = requests.get(url = hostname, timeout = 5)
- if response.status_code == 200:
- print('{} - is online'.format(hostname))
- else:
- print('{} - is down'.format(hostname))
- import requests
- url = [
- 'https://www.google.com',
- 'https://www.youtube.com',
- 'https://www.nosite.noite',
- 'https://www.fsf.org',
- 'https://www.stackoverflow.com',
- 'https://www.gnu.org',
- 'https://www.kernel.org'
- ]
- for hostname in url:
- try:
- response = requests.get(url = hostname, timeout = 5)
- if response.status_code == 200:
- print('{} - is online'.format(hostname))
- else:
- print('{} - is down'.format(hostname))
- except:
- pass
- import requests
- url = [
- 'https://www.google.com',
- 'https://www.youtube.com',
- 'https://www.nosite.noite',
- 'https://www.fsf.org',
- 'https://www.stackoverflow.com',
- 'https://www.gnu.org',
- 'https://www.kernel.org'
- ]
- for hostname in url:
- try:
- response = requests.get(url = hostname, timeout = 5)
- if response.status_code == 200:
- print('{} - is online'.format(hostname))
- else:
- print('{} - is down'.format(hostname))
- except:
- print('{} - Error'.format(hostname))
- ## Running shell program
- ##
- import subprocess
- result = subprocess.call('mkdir /tmp/mydir', shell = True)
- print(result)
- ! ls -l /tmp/
- ##
- import subprocess
- cp = input('Enter the file to copy to /tmp/mydir: ')
- cmd = "cp {} /tmp/mydir".format(cp)
- result = subprocess.call(cmd,shell = True)
- print(result)
- ! ls -l /tmp/mydir/
- ## if else vs if elif
- s = 'a1b2c3@#$'
- alpha_c = 0
- digit_c = 0
- special_c = 0
- for item in s:
- if item.isalpha():
- alpha_c += 1
- if item.isdigit():
- digit_c += 1
- else:
- special_c += 1
- print('Alpha - {}, Digit - {}, Special chara - {}'.format(alpha_c,digit_c,special_c))
- ###
- s = 'a1b2c3@#$'
- alpha_c = 0
- digit_c = 0
- special_c = 0
- for item in s:
- if item.isalpha():
- alpha_c += 1
- elif item.isdigit():
- digit_c += 1
- else:
- special_c += 1
- print('Alpha - {}, Digit - {}, Special chara - {}'.format(alpha_c,digit_c,special_c))
- ## While
- s = 0
- number = 1
- while number <= 10:
- print(number)
- number += 1
- s = 0
- number = 1
- while number <= 10:
- s = s + number
- number += 1
- print(s)
- ## sorting
- number_l = [5,3,1,8,7,4,9]
- number_t = (5,3,1,8,7,4,9)
- r = sorted(number_l)
- print(r)
- r = sorted(number_t, reverse=True)
- print(r)
- name = 'prince'
- print(sorted(name))
- names = ['richard','prince','sam','kevin','jobin']
- print(sorted(names))
- ## Sorting based on key length
- names = ['richard','prince','sam','kevin','jobin']
- r = sorted(names,key = len)
- print(r)
- ## Sorting based on salary
- names = ['richard-25000','prince-20000','sam-40000','kevin-15000','jobin-50000']
- def salary(item):
- return int(item.split('-')[-1])
- r = sorted(names, key = salary)
- print(r)
- ##
- person = [['sam',25],['prince',23],['kevin',30],['john',50]]
- def age(x):
- return x[-1]
- r = sorted(person, key = age)
- print(r)
- ##
- d = {'alex': 25, 'prince': 23, 'sam': 30,'kevin': 33}
- r = sorted(d.values())
- print(r)
- ##
- d = {'alex': 25, 'prince': 23, 'sam': 30,'kevin': 33}
- def get_age(item):
- return item[-1]
- r = sorted(d.items(),key = get_age)
- print(r)
- ##
- access_log = "access.log"
- logfile = open(access_log,"r")
- ipcounter = {}
- for line in logfile:
- host = line.split()[0]
- if host not in ipcounter:
- ipcounter[host] = 1
- else:
- ipcounter[host] += 1
- def ip_hit(item):
- return item[-1]
- r = sorted(ipcounter.items(), key = ip_hit)
- print(r[-1])
- print(r[-10:])
- ##
- for item in r[-10:]:
- ip,freq = item
- print("{:20} - {}".format(ip,freq))
Advertisement
Add Comment
Please, Sign In to add comment