Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
arytmw authored Aug 16, 2020
1 parent e1a4919 commit 4e150a8
Show file tree
Hide file tree
Showing 19 changed files with 345 additions and 0 deletions.
21 changes: 21 additions & 0 deletions append_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python3.6

import json

def write_json(data, filename = 'example_2.json'):
with open(filename, 'w') as f:
json.dump(data, f, indent=4)

with open('example_2.json') as json_file:
data = json.load(json_file)

temp = ['quiz']

y = { 'sale_country': 'India',
'currency': 'INR',
'type': 'fruit'
}

temp.append(y)

write_json(data)
12 changes: 12 additions & 0 deletions download_large_files
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python3.6

import requests

url = "https://ia803208.us.archive.org/6/items/j.-r.-r.-tolkien-la-comunidad-del-anillo-i/J.R.R.%20Tolkien%20La%20Comunidad%20del%20anillo%20I.pdf"

res = requests.get(url, stream = True)

with open('lotr.pdf','wb') as myfile:
for chunk in res.iter_content(chunk_size=1024):
if chunk:
myfile.write(chunk)
9 changes: 9 additions & 0 deletions download_simple_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3.6

import requests

url = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"

res = requests.get(url)
with open('python_logo.png','wb') as myfile:
myfile.write(res.content)
11 changes: 11 additions & 0 deletions edit_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3.6

import json

new_data = {"e":5}

with open("sample.json","r+") as myfile:
data = json.load(myfile)
data.update(new_data)
myfile.seek(0)
json.dump(data,myfile)
13 changes: 13 additions & 0 deletions edit_json2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3.6

import json

new_data = {"name":"waseem"}

with open('sample.json') as myfile:
data = json.load(myfile)

data.update(new_data)

with open("sample.json","w") as myfile:
json.dump(data,myfile)
8 changes: 8 additions & 0 deletions extract_tar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python3.6

import tarfile

filename = 'sample.tar.gz'

tf = tarfile.open(filename)
tf.extractall()
40 changes: 40 additions & 0 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@


from fabric.api import * #applicaation programming interface

# uname -r

env.user = 'python'
env.hosts = 'vsftpd'

def check_kernel():
run('uname -r')

def update_all():
sudo('yum update -y')

def result():
sudo('ls -l /var/www/')

def dir_create():
local('mkdir /tmp/fabricdir')

env.roledefs = {
'webservers': ['localhost','10.0.0.8']
}

@roles ('webservers')
def check_uptime():
run('uptime')

def copy():
put('/etc/hosts','/home/python/hostsfab', mode=777)

def port_number():
prompt('Which port?', default=80, validate=int)

def reboot():
reboot(wait=30)

def download():
get('/etc/passwd','/tmp/passwdvsftpd')
9 changes: 9 additions & 0 deletions glob
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3.6

import glob

print("Name of a file")
for name in glob.glob('/etc/p*'):
print(name)


9 changes: 9 additions & 0 deletions glob2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3.6

import glob

print("using glob.glob")
files = glob.glob('/etc/**/*', recursive=True)

for file in files:
print(file)
7 changes: 7 additions & 0 deletions modtime
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3.6
import os
from datetime import datetime

lastmodified = os.stat('myfile.txt').st_mtime

print(datetime.fromtimestamp(lastmodified))
31 changes: 31 additions & 0 deletions mysql_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3.6

import pymysql
import json

file = 'example.json'

json_data = open(file).read()
json_obj = json.load(json_data)

def validate_string(val):
if val != None:
if type(val) is int:
return str(val).encode('utf-8')
else:
return val

con = pymysql.connect(host = 'hostname', user = 'username', password = 'password', db = 'dbname')
con_cur = con.cursor()

#person, year and client

for i, item in enumerate(json_obj):
person = validate_string(item.get("person", None))
year = validate_string(item.get("year", None))
client = validate_string(item.get("client", None))

cursor.execute("INSERT INTO tbname (person, year, client)) VALUES (%s, %s, %s)".(person, year, client))

con.commit()
con.close()
23 changes: 23 additions & 0 deletions mysql_python
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python3.6

import subprocess
import boto3

#mysqldump -h db@192.168.0.1 -u username -p password

username = 'username'
password = 'password'
database = 'database_url'
db_name = 'db@192.168.0.1'

with open('mybackup.sql,'w') as myfile:
c = subprocess.Popen(['mysqldumb','-h', db_name, '-u', username, '-p',password],
stdout=myfile, shell=True)

with open('backup.sql','w') as output:
command = subprocess.Popen(['mysqldump','-u', username, '-p',password, database],
stdout=output, shell=True)

s3 = boto3.client('s3')
with open('backup.sql', 'rb') as f:
s3.upload_fileobj(f, 'bucket_name', 's3backup.sql')
9 changes: 9 additions & 0 deletions read_json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3.6

import json

with open('example_2.json', 'r') as openfile:
myjson = json.load(openfile)

print(myjson)
#print(type(json_obj))
9 changes: 9 additions & 0 deletions readcsv_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3.6

import csv

with open('data.csv', 'r') as myfile:
csvfile = csv.reader(myfile)

for lines in csvfile:
print(lines)
6 changes: 6 additions & 0 deletions readcsv_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python3.6

import pandas

csvfile = pandas.read_csv('data.csv')
print(csvfile)
9 changes: 9 additions & 0 deletions readcsv_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3.6

import csv

with open('data.csv', 'r') as myfile:
csvfile = csv.DictReader(myfile)

for lines in csvfile:
print(lines)
8 changes: 8 additions & 0 deletions readtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python3.6

import tarfile

filename = 'sample.tar.gz'

tf = tarfile.open(filename)
print(tf.getnames())
15 changes: 15 additions & 0 deletions readzip
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3.6

from zipfile import ZipFile
import datetime

filename = "sample.zip"

with ZipFile(filename, 'r') as zip:
for info in zip.infolist():
print(info.filename)
print('\tModified:\t' + str(datetime.datetime(*info.date_time)))
print('\tSystem:\t\t' + str(info.create_system) + '(0 = Windows, 3 = Linux)')
print('\tZip version:\t' + str(info.create_version))
print('\tCompressed:\t' + str(info.compress_size) + 'bytes')
print('\tUncompressed:\t' + str(info.file_size) + 'bytes' )
96 changes: 96 additions & 0 deletions sysinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3.6

import psutil
import platform
from datetime import datetime


# 1253656 --> 1.20MB
# 1253656678 ---> 1.17 GB
def get_size(bytes, suffix="B"):
factor = 1024
for unit in ["","K","M","G","T","P"]:
if bytes < factor:
return f"{bytes:2f}{unit}{suffix}"
bytes /= factor

print("="*40, "System Information", "="*40)

uname = platform.uname()
print(f"System: {uname.system}")
print(f"Name: {uname.node}")
print(f"Release: {uname.release}")
print(f"Version: {uname.version}")
print(f"Machine: {uname.machine}")
print(f"Processor: {uname.processor}")
#print(f"System: {platform.uname().system}")

print("="*40, "Boot Time", "="*40)
boot_time_timestamp = psutil.boot_time()
bt = datetime.fromtimestamp(boot_time_timestamp)

print(f"Boot time: {bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second}")

print("="*40, "CPU Info", "="*40)
print("Physical cores:", psutil.cpu_count())

cpufreq = psutil.cpu_freq()
print(f"Current Frequency: {cpufreq.current} Mhz")
#print(f"Maximum Frequency: {cpufreq.max:.2f} Mhz")

print("="*40, "Memory Information", "="*40)
sysmem = psutil.virtual_memory()
print(f"Total Memory: {get_size(sysmem.total)}")
print(f"Available Memory: {get_size(sysmem.available)}")
print(f"Used Memory: {get_size(sysmem.used)}")
print(f"Percentage: {sysmem.percent}%")
swap = psutil.swap_memory()
print(f"Total Swap: {get_size(swap.total)}")
print(f"Free Swap: {get_size(swap.free)}")
print(f"Used Swap: {get_size(swap.used)}")
print(f"Percentage: {swap.percent}%")

print("="*40, "Disk Information", "="*40)
part = psutil.disk_partitions()
#partition_usage = psutil.disk_usage(partition.mountpoint)
for partition in part:
print(f"Device: {partition.device}")
print(f"MountPoint: {partition.mountpoint}")
print(f"File System Type: {partition.fstype}")
try:
partition_usage = psutil.disk_usage(partition.mountpoint)
except PermissionError:
continue
print(f"Total Size: {get_size(partition_usage.total)}")
print(f"Used Size: {get_size(partition_usage.used)}")
print(f"Free Size: {get_size(partition_usage.free)}")
print(f"Percentage: {get_size(partition_usage.percent)}%")

print("="*40, "Network Information", "="*40)

if_addrs = psutil.net_if_addrs()

for interface_name, interface_addresses in if_addrs.items():
for address in interface_addresses:
print(f"Interface: {interface_name}")
if str(address.family) == 'AddressFamily.AF_INET':
print(f"IP Address: {address.address}")
print(f"Netmask: {address.netmask}")
print(f"Broadcast IP: {address.broadcast}")

















0 comments on commit 4e150a8

Please sign in to comment.