Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def hello():
- print('Hello world')
- hello()
- hello()
- hello()
- ##
- def calc():
- x = 5
- y = 3
- total = x+y
- print(total)
- calc()
- calc()
- calc()
- ## Argument
- def calc(first,second):
- x = first
- y = second
- total = x + y
- print(total)
- calc(5,3)
- calc(10,15)
- calc(9,8)
- ##
- def calc(first,second):
- x = first
- y = second
- total = x + y
- print(total)
- result = calc(5,3)
- print(result)
- ## Return
- def calc(first,second):
- x = first
- y = second
- return x + y
- result = calc(10,10)
- print(result)
- ##
- def mylen(iterable):
- l = 0
- for item in iterable:
- l += 1
- return l
- print(mylen([1,2,3]))
- ##
- print(mylen('prince'))
- ## Multiple return
- def mylen(iterable):
- l = 0
- for item in iterable:
- l += 1
- return 'Hello'
- return l
- print(mylen('prince'))
- ##
- def mycal(x,y):
- r1 = x + y
- r2 = x * y
- return [r1,r2]
- result = mycal(10,10)
- print(result)
- ## Default argument
- def calc(x,y):
- return x + y
- r = calc(5,3)
- print(r)
- def calc(x=0,y=0):
- return x + y
- r = calc(5)
- print(r)
- r = calc(5,4)
- print(r)
- ## SSh
- def sshconfig(user,port,host):
- return "ssh -p {} {}@{}".format(port,user,host)
- r = sshconfig("ec2-user",22,"3.3.3.3")
- print(r)
- ##
- def sshconfig(host,port=22,user='root'):
- return "ssh -p {} {}@{}".format(port,user,host)
- r = sshconfig('1.1.1.1')
- print(r)
- r = sshconfig('1.1.1.1',2020)
- print(r)
- r = sshconfig('1.1.1.1',2020,'ec2-user')
- print(r)
- r = sshconfig('1.1.1.1','alex')
- print(r)
- ## Keyword argument
- def sshconfig(host,port=22,user='root'):
- return "ssh -p {} {}@{}".format(port,user,host)
- r = sshconfig(user = 'sam',port= '2323',host = '109.145.23.99')
- print(r)
- ## Requests
- import requests
- ip_url = "https://api.ipgeolocation.io/ipgeo?apiKey=31fcd226b4674a85ac09fe288740ef9b&ip=8.8.8.8"
- response = requests.get(ip_url)
- print(response.headers)
- print()
- print(response.status_code)
- print()
- print(response.text)
- ## Json
- import requests
- ip_url = "https://api.ipgeolocation.io/ipgeo?apiKey=31fcd226b4674a85ac09fe288740ef9b&ip=8.8.8.8"
- response = requests.get(url = ip_url)
- json_data = response.json()
- print(json_data)
- print(json_data['ip'])
- import requests
- def get_country(ip = None):
- if ip != None:
- ip_url = "https://api.ipgeolocation.io/ipgeo?apiKey=31fcd226b4674a85ac09fe288740ef9b&ip={}".format(ip)
- response = requests.get(url = ip_url)
- json_data = response.json()
- return json_data['country_name']
- else:
- return None
- r = get_country('3.3.3.3')
- print(r)
- r = get_country('9.9.9.9')
- print(r)
- ## Module
- import ipcountry
- import logparser
- access_log = "access.log"
- logfile = open(access_log,"r")
- ipcounter = {}
- for line in logfile:
- logParts = logparser.parser(line)
- host = logParts['host']
- status = logParts['status']
- if status == "404":
- if host not in ipcounter:
- ipcounter[host] = 1
- else:
- ipcounter[host] = ipcounter[host] + 1
- for ip in ipcounter:
- freq = ipcounter[ip]
- if freq >= 50:
- country = ipcountry.get_country(ip = ip)
- out = "{:20} - {:6} - {} ".format(ip,freq,country)
- print(out)
- ## iseven
- def iseven(x):
- if x % 2 == 0:
- return True
- else:
- return False
- ##
- def isodd(x):
- if x % 2 != 0:
- return True
- else:
- return False
- number = iseven(8)
- print(number)
- number = iseven(9)
- print(number)
- number = isodd(9)
- print(number)
Advertisement
Add Comment
Please, Sign In to add comment