コード例 #1
0
ファイル: birdway.py プロジェクト: nsknojj/Birdway-Server
def main():
    arg(sys.argv)
    if not Files.exist(host_filename):
        print 'No such file'
        sys.exit(3)
    Files.lock[host_filename] = threading.Lock()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ip = ''
    port = int()
    while True:
        try:
            port = int(random() * 2000 + 2000)
            s.bind((ip, port))
            break
        except:
            pass
    s.listen(10)
    ip = socket.gethostbyname(socket.gethostname())
    print 'Host:', socket.gethostbyname_ex(socket.gethostname())
    print 'Port:', port
    print 'File name:', host_filename
    print 'Password:'******'Waiting for connection...'

    t_save = threading.Thread(target=save_thread)
    t_save.setDaemon(True)
    t_save.setName('save_thread')
    t_save.start()
    t_syn = threading.Thread(target=syn_thread)
    t_syn.setDaemon(True)
    t_syn.setName('syn_thread')
    t_syn.start()

    while True:
        sock, addr = s.accept()
        lock[sock] = threading.Lock()
        t = threading.Thread(target=tcp_link, args=(sock, addr))
        t.setDaemon(True)
        t.start()

    del Files.lock[host_filename]
コード例 #2
0
ファイル: Main.py プロジェクト: nsknojj/Birdway-Server
def tcp_link(skt, addr):
    print('Accept new connection from %s:%s...' % addr)
    # sock.send(b'Welcome!')
    data = ''
    login = 0
    upload_filename = ''
    while True:
        if not skt:
            break
        data = data + skt.recv(1024)
        # time.sleep(1)
        if not data or data.decode('utf-8') == 'exit':
            break

        # sendToAll(skt, data)
        # data = ''
        while True:
            try:
                pos = data.index('\n')
            except:
                break
            print [data[:pos]]
            msg = json.loads(data[:pos])

            reply = dict()
            query = msg['type']
            reply['type'] = query

            if query == 'register':
                error = Users.insert_user(msg['user'], msg['password'])
                reply['error'] = error
                send(skt, json.dumps(reply))

            elif query == 'login':
                username = msg['name']
                u = Users.get_user(username)
                if len(u) < 2:
                    error = 1
                elif u[2] != hash(msg['password']):
                    error = 1
                else:
                    error = 0
                    login = 1
                    onlineList.append(skt)
                    name_list[skt] = username
                reply['error'] = error
                send(skt, json.dumps(reply))

            elif login == 0:
                continue

            if query == 'logout':
                login = 0
                onlineList.remove(skt)
                break

            elif query == 'create':
                filename = msg['filename']
                error = Files.create_file(filename)
                reply['error'] = error
                if error == 0:
                    Auth.change(filename, name_list[skt], 2, name_list[skt])
                send(skt, json.dumps(reply))

            elif query == 'edit':
                filename = msg['filename']
                if not Files.exist(filename):
                    reply['error'] = 1
                    send(skt, json.dumps(reply))
                    continue
                if not Auth.have_edit_auth(filename, name_list[skt]):
                    reply['error'] = 2
                    send(skt, json.dumps(reply))
                    continue
                reply['error'] = 0
                send(skt, json.dumps(reply))
                reply.clear()
                reply['type'] = 'edit_content'
                reply['filename'] = 'filename'
                reply['isend'] = 1
                content = dict()
                content['oldRange'] = {'start': {'row': 0, 'column': 0}, 'end': {'row': 0, 'column': 0}}
                content['oldText'] = ''
                (file_content, r, c) = Files.edit_file(filename)
                content['newText'] = file_content
                content['newRange'] = {'start': {'row': 0, 'column': 0}, 'end': {'row': r, 'column': c}}
                reply['content'] = content
                send(skt, json.dump(reply))

                Files.add_editor(filename, skt)

            elif query == 'upload':
                filename = msg['filename']
                error = Files.create_file(filename)
                reply['error'] = error
                send(skt, json.dumps(reply))
                if error == 1:
                    continue
                upload_filename = filename
                Auth.change(filename, name_list[sock], 2, name_list[skt])

            elif query == 'upload_content':
                if upload_filename == '':
                    continue
                Files.up_file(upload_filename, msg['content']['newText'])
                upload_filename = ''
                pass

            elif query == 'change_auth':
                filename = msg['filename']
                other_name = msg['other_name']
                if not Files.exist(filename):
                    reply['error'] = 1
                    send(skt, json.dumps(reply))
                    continue
                if not Auth.have_manage_auth(filename, other_name):
                    reply['error'] = 2
                    send(skt, json.dumps(reply))
                    continue
                reply['error'] = 0
                Auth.change(filename, other_name, msg['auth'])
                send(skt, json.dumps(reply))
                pass

            elif query == 'rm':
                filename = msg['filename']
                if not Files.exist(filename):
                    reply['error'] = 1
                    send(skt, json.dumps(reply))
                    continue
                if not Auth.have_manage_auth(filename, name_list[skt]):
                    reply['error'] = 2
                    send(skt, json.dumps(reply))
                    continue
                reply['error'] = 0
                Files.delete_file(filename)
                send(skt, json.dumps(reply))

            elif query == 'ls':
                reply['list'] = Auth.get_edit_list(name_list[skt])
                send(skt, json.dumps(reply))
                pass

            elif query == 'close':
                Files.del_editor(msg['filename'], skt)

            elif query == 'modify':
                modify = msg['content']
                filename = msg['filename']
                send_to_all(skt, json.dumps(modify), filename)
                Files.change_file(filename, modify)

    skt.close()
    print('Connection from %s:%s closed.' % addr)