Za enunt


In [ ]:


Za recapitulare

SOCKETS


In [ ]:
#!/usr/bin/python           # This is server.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   print 'looping'
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection
   break


looping

In [ ]:
#!/usr/bin/python           # This is client.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.

print 'connecting'

s.connect((host, port))
print s.recv(1024)
s.close                     # Close the socket when done

setup_db.py


In [1]:
import sqlite3
conn = sqlite3.connect('example.db')

c = conn.cursor()

# Create table
c.execute('''CREATE TABLE location
             (id_location text, location text)''')

c.execute('''CREATE TABLE files
             (id_location text, id_file text)''')

c.execute('''CREATE TABLE file_info
             (id_file text, file_name text, file_size text, creation_time text, file_md5 text)''')

# Save (commit) the changes
conn.commit()

# Close the connection
# => any change that has not been commited will be lost
conn.close()

add_files.py


In [2]:
import sqlite3
conn = sqlite3.connect('example.db')

c = conn.cursor()


import os
import hashlib
import time

def get_file_md5(filePath):
	h = hashlib.md5()
	h.update(open(filePath,"rb").read())
	return h.hexdigest()

def get_file_sha256(filePath):
	h = hashlib.sha256()
	h.update(open(filePath,"rb").read())
	return h.hexdigest()


def get_dir_data(dir_path):
	dir_path = os.path.realpath(dir_path)
	
	#print next(os.walk(dir_path))[2]
	#print os.path.basename(dir_path)
	
	id_location = 0
	id_file = 0
	for dir_file in next(os.walk(dir_path))[2]:
		file_name = dir_file
		file_md5 = get_file_md5(dir_file)
		file_sha256 = get_file_sha256(dir_file)
		file_size = os.path.getsize(dir_file)
		
		file_time = time.gmtime(os.path.getctime(dir_file))
		file_formatted_time = time.strftime("%Y-%m-%d %I:%M:%S %p", file_time)
		file_path = os.path.realpath(dir_file)
		

		location_values = (id_location, file_path)
		c.execute("INSERT INTO location VALUES (?, ?)", location_values)

		files_values = (id_location, id_file)
		c.execute("INSERT INTO files VALUES (?, ?)", files_values)

		file_info_values = (id_file, file_name, file_size, file_formatted_time, file_md5)
		c.execute("INSERT INTO file_info VALUES (?, ?, ?, ?, ?)", file_info_values)

		id_location += 1
		id_file += 1


get_dir_data('./')

# Save (commit) the changes
conn.commit()

conn.close()

query_db.py


In [3]:
import sqlite3
conn = sqlite3.connect('example.db')

c = conn.cursor()

print('\n\n*****location table****\n')
'''
for row in c.execute('SELECT * FROM location'):
	# print(c.fetchall())
	print(row)
'''
'''
c.execute('SELECT * FROM location')
print(c.fetchall())
'''
c.execute('SELECT * FROM location')
copy = c.fetchall()
for row in copy:
	print row


print('\n\n*****files table****\n')
c.execute('SELECT * FROM files')
copy = c.fetchall()
for row in copy:
	print row

print('\n\n*****file_info table****\n')
c.execute('SELECT * FROM file_info')
copy = c.fetchall()
for row in copy:
	print row

conn.close()



*****location table****

(u'0', u'E:\\github\\ml-stuff\\labs-python\\data.json')
(u'1', u'E:\\github\\ml-stuff\\labs-python\\example.db')
(u'2', u'E:\\github\\ml-stuff\\labs-python\\labs-python.json')
(u'3', u'E:\\github\\ml-stuff\\labs-python\\net-client.py')
(u'4', u'E:\\github\\ml-stuff\\labs-python\\net-server.py')
(u'5', u'E:\\github\\ml-stuff\\labs-python\\py-extra-del.ipynb')
(u'6', u'E:\\github\\ml-stuff\\labs-python\\PythonLab-5.ipynb')
(u'7', u'E:\\github\\ml-stuff\\labs-python\\PythonLab-6.ipynb')
(u'8', u'E:\\github\\ml-stuff\\labs-python\\PythonLab-7.ipynb')
(u'9', u'E:\\github\\ml-stuff\\labs-python\\PythonLabs.ipynb')
(u'10', u'E:\\github\\ml-stuff\\labs-python\\README.md')
(u'11', u'E:\\github\\ml-stuff\\labs-python\\Test3.ipynb')


*****files table****

(u'0', u'0')
(u'1', u'1')
(u'2', u'2')
(u'3', u'3')
(u'4', u'4')
(u'5', u'5')
(u'6', u'6')
(u'7', u'7')
(u'8', u'8')
(u'9', u'9')
(u'10', u'10')
(u'11', u'11')


*****file_info table****

(u'0', u'data.json', u'212', u'2016-10-25 09:53:20 PM', u'1d0ff2468e8841c491b7bddbe305633e')
(u'1', u'example.db', u'4096', u'2017-01-24 10:16:53 PM', u'2df7c31889f1e0858eaddee895774d86')
(u'2', u'labs-python.json', u'2665', u'2016-11-15 01:46:10 PM', u'21a613c94c46eaae6924052aca0deab1')
(u'3', u'net-client.py', u'434', u'2016-11-26 11:08:06 AM', u'169bd912ecae9cf402791308fcd2f3c3')
(u'4', u'net-server.py', u'1561', u'2016-11-26 11:07:01 AM', u'4c11006c69d969fa3adabcc681252045')
(u'5', u'py-extra-del.ipynb', u'2044', u'2016-10-25 11:23:46 PM', u'dd1a719843faa3e134b287a13a90b0e0')
(u'6', u'PythonLab-5.ipynb', u'6165', u'2016-11-14 10:47:44 PM', u'308dbb376621834d935220de8d20a0b0')
(u'7', u'PythonLab-6.ipynb', u'9852', u'2016-11-15 07:19:41 AM', u'23e04ab85ed174d94907f843b018d2cb')
(u'8', u'PythonLab-7.ipynb', u'7255', u'2016-11-15 12:43:25 PM', u'b27ae18d6c897bc6edea4d6b9cad3738')
(u'9', u'PythonLabs.ipynb', u'26695', u'2016-10-19 05:08:43 AM', u'113d76e20cec5e2811a8169ac350a6c6')
(u'10', u'README.md', u'405', u'2016-12-14 06:48:26 AM', u'd6242f10d2277560ca5da395abcbee69')
(u'11', u'Test3.ipynb', u'2310', u'2017-01-24 10:05:23 PM', u'7a62ce67cd4707ca9362358e6467c4cd')

In [13]:
import urllib
import re

params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
respData = f.read()

#print respData

count = 0
paragraphs = re.findall(r'<a .*>(.*)</a>',str(respData))
for eachP in paragraphs:
    print(eachP)
    if count == 5:
        break
    count += 1


Sign In
All
Go
Music
Posters
Photography

In [ ]: