-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyServer.py
More file actions
110 lines (97 loc) · 3.53 KB
/
MyServer.py
File metadata and controls
110 lines (97 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import socket
import os
import time
from urllib.parse import *
from threading import *
ENC = 'UTF-8'
MainWebPage = "./Content/MainPage.html"
MusPath = r"/media/florian/01D1F32F6143C9A0/Music" #/media/florian/Elements/music"
WinMusPath = r"D:\Music\Sweet Dreams"
AudioMask = "<li>{0}<br/><audio controls preload='none'><source src='{1}' type='audio/mpeg'></audio></li>"
FolderRefMask = '<li><a href="{0}">{1}</a></li>'
PM = "HTTP/1.1 200 OK\r\n\r\n"
AudioFormats = ['.mp3', '.m4a', '.wav']
iconF = './Content/favicon.ico'
noImg = "./Content/NoImg.jpg"
def getfile(filepath):
with open(filepath, 'r') as fil:
text = fil.read()
fil.close()
return text
def getBin(Bpath):
with open(Bpath,'rb') as bFil:
bData = bFil.read()
bFil.close()
return bData
class MyThread (Thread):
MainPageMask = ""
def __init__(self,name, connection, address):
Thread.__init__(self)
self.name = name
self.c = connection
self.addr = address
self.data = ''
def run (self):
print ( 'run thread', self.name)
self.data = dec (self.c.recv(1024))
#print(self.data)
if ('GET' in self.data):
try:
self.ServeBrowser()
except Exception as ex:
print("Exception: ",ex)
self.c.close ()
print('Closed {}, name = {}'.format( self.addr, self.name))
def ServeBrowser(self):
pathX = unquote(self.data.split(' ')[1])
#print(getText(self.data))
resData = None
print ('Browser on {} request, name = {}'.format(self.addr, self.name), pathX)
if 'Content' in self.data:
self.c.send(enc(PM)+getBin('.'+pathX))
return
else:
FullPath = os.path.join(MusPath,pathX[1:])
print(FullPath, pathX)
if ('.mp3' in pathX) or ('.m4a' in pathX): #need to Check if requested file is a music file
resData = getBin(FullPath)
elif ('.jpg' in pathX):
try:
resData = getBin(FullPath)
except:
resData = getBin(noImg)
elif 'favicon' in pathX:
resData = getBin(iconF)
else:
print('============================',pathX,'is not a file ==========')
liMus = list(filter(lambda x: (".mp3" in x) or ('.m4a' in x), os.listdir(FullPath)))
p = '\n'.join(list(map(lambda x : AudioMask.format(x,quote(pathX+x)), liMus)))
liDirs = list(filter(lambda x: not((".mp3" in x) or ('.m4a' in x)), os.listdir(FullPath)))
print(FullPath, liMus)
print(FullPath, liDirs)
if pathX[-1] != '/': pathX+='/'
wpage = MyThread.MainPageMask.format(pathX+'folder.jpg',pathX,p)
resData= enc(wpage)
if len(resData) > (1024*512):
self.c.sendall(enc(PM)+resData)
else:
self.c.send (enc(PM)+resData)
def dec(bytMsg):
return bytMsg.decode (ENC)
def enc (strMsg):
return strMsg.encode(ENC)
def Main():
host = ''
port = 80
MyThread.MainPageMask = getfile(MainWebPage)
global s
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)
global work
while True:
c, addr = s.accept()
thr = MyThread (addr, c, addr)
thr.start()
Main()