Skip to content

Instantly share code, notes, and snippets.

@kozmonaut
kozmonaut / rss-parse.py
Created February 11, 2015 14:08
Parse RSS feed and store it to file
from urllib import urlopen
from xml.etree.ElementTree import parse
rss_url = urlopen('http://www.dropsql.com/feed')
xml = parse(rss_url)
f = open('dropsql-feeds.txt', 'w')
# Looks for all item elements under channel tag
for item in xml.iterfind('channel/item'):
@kozmonaut
kozmonaut / network.py
Created January 30, 2015 08:18
Networking workbook in py
# Import socket library
import socket
# Import hexlify for ip address conversion
from binascii import hexlify
# Use NTP protocol
import ntplib
from time import ctime
# Print hostname
hostname = socket.gethostname()
@kozmonaut
kozmonaut / geocoder.py
Created January 30, 2015 08:15
Extract company address and coordinates using geocoder
import geocoder
def company(name):
results = geocoder.google(name)
print ("Coordinates: Latitude - %s, Longitude - %s" %(results.lat, results.lng))
print ("Address: %s" % results.address)
if __name__ == '__main__':
print ("Type name of the place:")
name = raw_input()
@kozmonaut
kozmonaut / scrape.py
Created January 30, 2015 08:12
Extract links from "beautiful soup"
from bs4 import BeautifulSoup
import csv
import re
import requests
# Copy URL you wanna crawl
print ("Copy the link you wanna scrap:")
url = raw_input("->")
# Fetch url
request = requests.get(url)
@kozmonaut
kozmonaut / quicksort.py
Last active January 15, 2021 16:21
Quicksort in Python
def quicksort(list):
# Define lists
less = []
equal = []
greater = []
if len(list) > 1: # Check if list have more than 1 element
pivot = list[0] # This is now pivot value
for number in list: # Go through numbers in list
@kozmonaut
kozmonaut / weather-api.py
Created January 30, 2015 08:03
Use OpenWeatherMap API with Python
import urllib
import json
# Define url you wanna fetch
url = "http://api.openweathermap.org/data/2.5/find?q=London&units=metric"
# Fetch url
response = urllib.urlopen(url)
# Load url response to json
@kozmonaut
kozmonaut / euclidean-algorithm.py
Last active January 15, 2021 16:22
Euclidean algorithm - Find a greatest common divisor
def gcd(a,b): # Define function with two parameters
while b != 0: # Run loop till 'b' value is 0
if a > b: # 'a' value must be larger than 'b'
r = a % b # Remainder value
a = b # Assign 'b' to 'a'
b = r # Asign remainder value to 'b'
return a # Return result
@kozmonaut
kozmonaut / linux-system-info
Last active January 15, 2021 16:28
Extract Linux system data using command line
Time: /bin/date
Login: users | awk '{print $1}'
Architecture: uname -m
Kernel version: uname -v -r
Processor: cat /proc/cpuinfo | grep \"name\" | cut -d : -f2 | uniq
Operating system: cat /etc/*-release | grep \"PRETTY_NAME\" | cut -d \"=\" -f2 | uniq
Interfaces: ip link show | grep 'eth\|wlan' | awk '{print $1,$2}'
IP addresses: ip a | grep 'eth\|wlan' | awk '{print $1,$2}'
Transmitted: /sbin/ifconfig eth0 | grep 'TX bytes' | awk '{print $7,$8}'
Recieved: /sbin/ifconfig eth0 | grep 'RX bytes' | awk '{print $3,$4}'
@kozmonaut
kozmonaut / linux-packages
Created April 14, 2014 11:44
Backup and restore installed packages inside Linux Debian
# Backup your packages list
# Get a packages list
dpkg --get-selections > ~/Package.list
# Copy list of repositories
sudo cp /etc/apt/sources.list ~/sources.list
# Export repo keys
sudo apt-key exportall > ~/Repo.keys
@kozmonaut
kozmonaut / hash-sha265.py
Created April 14, 2014 11:29
Hashing with SHA256
#!/usr/bin/env python3.0
import hashlib
import sys
algorithm = dict(
sha256 = lambda x: hashlib.sha256(x).hexdigest()
)
def usage():