Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
hw done
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Sep 30, 2018
1 parent ddf40c0 commit 4b9d103
Show file tree
Hide file tree
Showing 8 changed files with 3,360 additions and 12 deletions.
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,14 @@
"files.associations": {
"*.pyc": "python",
"*.pys": "python"
}
},
"spellright.language": [
"en",
"hu"
],
"spellright.documentTypes": [
"markdown",
"latex",
"plaintext"
]
}
6 changes: 3 additions & 3 deletions lesson02/example3_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def run(self):
"""

print "{} started!".format(self.getName()) # Thread-x started!
time.sleep(1) # Pretend to work for a second
time.sleep(2) # Pretend to work for a second
print "{} finished!".format(self.getName()) # "Thread-x finished!"


if __name__ == '__main__':
for x in range(4): # Four times...
for x in range(50): # Four times...
mythread = MyThread(name="Thread-{}".format(
x + 1)) # ...Instantiate a thread and pass a unique ID to it
mythread.start() # ...Start the thread
time.sleep(.6) # ...Wait 0.6 seconds before starting another
time.sleep(.5) # ...Wait 0.6 seconds before starting another
13 changes: 13 additions & 0 deletions lesson02/example4_subprocesses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
""" Example 4
Subprocesses
"""
import subprocess

subprocess.call(['echo', 'hello call'], shell=True)

PIPE_ECHO = subprocess.Popen(['echo', 'hello Popen'],
shell=True,
stdout=subprocess.PIPE)

print PIPE_ECHO.communicate()
118 changes: 110 additions & 8 deletions lesson02/task2_homework_alexa.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
""" Task 2 - Alexa-top-1m """

import csv
from itertools import islice
from subprocess import call
import json
import threading
import platform
import datetime
from itertools import islice
from subprocess import PIPE, Popen
from multiprocessing.pool import ThreadPool


def tail(p_file, number):
Expand Down Expand Up @@ -47,14 +51,112 @@ def head(p_file, number):
return list(islice(p_file, number))


class PingThread(threading.Thread):
""" Pinging Class
Arguments:
threading {[type]} -- [description]
"""

def __init__(self, target):
super(PingThread, self).__init__()
self.target = target

def run(self):
print "lol"


def ping(row):
""" Runs the ping command a certain times
Arguments:
identification {int} -- [description]
target {[type]} -- [description]
Returns:
[type] -- [description]
"""

ping_iter_arg = '-n'
if platform.system() == 'Windows':
ping_iter_arg = '-n'
elif platform.system() == 'Linux':
ping_iter_arg = '-c'

pipe = Popen(["ping", ping_iter_arg, "1", row[1]], shell=True, stdout=PIPE)
pipe.wait()
return (row[1], "Rank: " + str(row[0]) + pipe.communicate()[0].decode())


def traceroute(row):
""" runs a traceroute for the given target
Arguments:
identification {[type]} -- [description]
target {[type]} -- [description]
"""
traceroute_executable = 'tracert'
if platform.system() == 'Windows':
traceroute_executable = 'tracert'
elif platform.system() == 'Linux':
traceroute_executable = 'traceroute'

traceroute_hops_arg = '-h'
if platform.system() == 'Windows':
traceroute_hops_arg = '-h'
elif platform.system() == 'Linux':
traceroute_hops_arg = '-m'

pipe = Popen([traceroute_executable, traceroute_hops_arg, "2", row[1]],
shell=True,
stdout=PIPE)
pipe.wait()
return (row[1], "Rank: " + str(row[0]) + pipe.communicate()[0].decode())


def query(command, entries):
""" Runs the command on each entry in entries and
prints it out in a json file
Arguments:
command {function} -- the command to run
entries {[type]} -- [description]
Returns:
[array<str>] -- all the results of the pings
"""

pool = ThreadPool(processes=30)
results = pool.map(command, entries)

result_pings = {
"date": str(datetime.datetime.now()),
"system": str(platform.system()),
command.__name__ + 's': []
}
for target, ping_output in results:
entry = {}
entry['target'] = target
entry['output'] = ping_output
result_pings[command.__name__ + 's'].append(entry)

with open(command.__name__ + "s.json", "w") as write_file:
json.dump(
result_pings,
write_file,
indent=4,
ensure_ascii=True,
separators=(',', ': '))

pool.close()
pool.join()
return results


with open("./lesson02/top-1m.csv", 'rb') as csv_file:
HEAD = head(csv_file, 100)
TAIL = tail(csv_file, 100)
FILE_READER = csv.reader(HEAD, delimiter=',', dialect='excel')
for row in FILE_READER:
print row[0]
print row[1]
query(ping, csv.reader(HEAD + TAIL, delimiter=',', dialect='excel'))
query(traceroute, csv.reader(HEAD + TAIL, delimiter=',', dialect='excel'))

print platform.system() # Windows

call(["ping", "www.google.com"], shell=True)
806 changes: 806 additions & 0 deletions pings.json

Large diffs are not rendered by default.

806 changes: 806 additions & 0 deletions row_pings.json

Large diffs are not rendered by default.

Loading

0 comments on commit 4b9d103

Please sign in to comment.