Ejemplo n.º 1
0
 def handle(self):
     print("new connection!")
     while True:
         data = self.request.recv(1024)
         msg_type = requests.parse_type(data)
         if msg_type == requests.LIST:
             response = bytes(' '.join(names), 'utf-8')
         elif msg_type == requests.STATION:
             name = requests.parse_station(data)
             station = stations[names.index(name)]
             print("client choise is %s" % name)
             for title in station.titles:
                 response = requests.title_msg(title)
                 self.request.send(response)
                 self.request.recv(1024)
             response = requests.station_msg(station.addr[0])
         # self.request.send(response)
         # continue
         elif msg_type == requests.EXIT:
             print("client exit!")
             return
         else:
             print("this cannot be")
         self.request.sendall(response)
Ejemplo n.º 2
0
    signal.signal(signal.SIGINT, exterminate)

    tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_sock.connect((server_ip, server_port))
    show_help()
    while True:
        cmd = input()
        if cmd == '!help':
            show_help()
        elif cmd == '!list':
            tcp_sock.send(requests.list_msg())
            stations = tcp_sock.recv(1024).decode("utf-8")
            print(stations)
        elif cmd.split()[0] == '!station':
            tcp_sock.send(requests.station_msg(cmd.split()[1]))
            while True:
                song, ret = requests.parse_song(tcp_sock.recv(1024))
                if ret is None:
                    break
                tcp_sock.send(requests.list_msg())
                print(song)
            addr = song
            print(addr)
            udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
            udp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            udp_sock.bind(('', server_port))
            addr_bytes = struct.pack("=4sl", socket.inet_aton(addr), socket.INADDR_ANY)
            udp_sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, addr_bytes)
            udp_thread = threading.Thread(target=listen, args=(udp_sock,))
            udp_thread.daemon = True