diff options
author | jerous <jerous@gmail.com> | 2008-08-23 16:45:48 +0200 |
---|---|---|
committer | jerous <jerous@gmail.com> | 2008-08-23 16:45:48 +0200 |
commit | 49e765eadb497e0fd2d72b6e3c74edb921b072a8 (patch) | |
tree | 714e10beefd072b89bb1174fbf8095e6e8c3a977 /jmpd.py | |
parent | 59cccbf6289a63c06a51662aeecf9af6168804bb (diff) |
fumbling
Diffstat (limited to 'jmpd.py')
-rw-r--r-- | jmpd.py | 64 |
1 files changed, 46 insertions, 18 deletions
@@ -1,5 +1,6 @@ import socket from thread import start_new_thread +from traceback import print_exc import codecs import os @@ -7,34 +8,61 @@ from mutagen.easyid3 import EasyID3 from clSettings import Settings +VERSION='0.1' MUSIC_DIRECTORY="music_directory" ENCODING="UTF-8" class server: ssocket=None + _sockets=[] - def start(self, type=socket.SOCK_STREAM): - self.ssocket=socket.socket(socket.AF_INET, type) - start_new_thread(self._start_server, ()) + def __init__(self): + self._jmpd=jmpd() + self._sockets=[] + + def start(self, port, type=socket.SOCK_STREAM): + try: + self.ssocket=socket.socket(socket.AF_INET, type) + self._start_server(port) + except: + print_exc() + return - def _start_server(self): - self.ssocket.bind(('localhost', 6600)) + def _start_server(self, port): + self.ssocket.bind(('localhost', port)) self.ssocket.listen(5) self.ssocket.settimeout(None) + print("Started jmpd v%s" % (VERSION)) while True: csock,caddr=self.ssocket.accept() - start_new_thread(self.handle, (csock, caddr)) + self._sockets.append(csock) + #start_new_thread(self.handle, (csock, caddr)) + self.handle(csock, caddr) - def read(file): - return file.readline().strip() - def write(file, line): - file.write(line) + def _read(self, file): + line="" + while line=="": + line=file.readline() + return line.strip() + def _write(self, file, line): + file.write("%s\n" % (line)) def handle(self, csocket, caddr): - print("Incoming connection from ".caddr) + print("Incoming connection from %s" % (str(caddr))) file=csocket.makefile('rw', 0) - self.read(file) + self._write(file, 'hello jmpd %s'%(VERSION)) + + try: + while True: + line=self._read(file) + print line + except: + print_exc() + + def closeAll(self): + for socket in self._sockets: + socket.close() def _getName(line): return line[0:line.find(': ')] @@ -50,7 +78,6 @@ class jmpd: self._lib=[] self._settings=Settings("mpdconf") self.readLib() - print self._lib def writeLib(self): all_tags=tags @@ -61,7 +88,7 @@ class jmpd: fout.write("# jmpd\n") fout.write("begin info\n") fout.write("encoding: %s\n" % (encoding)) - fout.write("jmpd: 0.1\n") + fout.write("jmpd: %s\n" % (VERSION)) fout.write("end info\n") fout.write("\n") @@ -98,7 +125,7 @@ class jmpd: self._readLine(fin) # begin info encoding=_getValue(self._readLine(fin)) version=_getValue(self._readLine(fin)) - if version!="0.1": + if version!=VERSION: raise Exception("Invalid database file") self._readNext(fin, 'end info') @@ -186,6 +213,7 @@ class jmpd: for subdir in subdirs: self._update_rec(subdir) -cl=jmpd() -cl.update() -print cl._lib +from test import port +s=server() +s.start(port) + |