Example #1
0
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    is_first = True
    print "请求服务器连接"
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    data = RECIEVE_INIT_DATA % room_id
    send_data = unhexlify(data)
    while True:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((RECIEVE_SERVER_ADDRESS, RECIEVE_SERVER_PORT))
            sock.sendall(send_data)
        except socket.error:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
Example #2
0
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    global is_first
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    userid = int(100000000 * random.random())
    body = ('{"roomid": ' + str(room_id) + ', "uid": ' + str(userid) + '}')
    while True:
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address = get_server(room_id)
            sock.connect((address, 788))
            send_data = send_socket_data(sock, 16 + len(body), 16, 1, 7, 1,
                                         body)
        except socket.error as exc:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
Example #3
0
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    is_first = True
    print "请求服务器连接"
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    data = RECIEVE_INIT_DATA % room_id
    send_data = unhexlify(data)
    while True:
        sock = socket.socket(socket.AF_INET,
                             socket.SOCK_STREAM)
        try:
            sock = socket.socket(socket.AF_INET,
                                 socket.SOCK_STREAM)
            sock.connect(
                (RECIEVE_SERVER_ADDRESS, RECIEVE_SERVER_PORT))
            sock.sendall(send_data)
        except socket.error:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()
Example #4
0
def generate_socket(room_id):
    """Generate the socket to communicate with Bilibili Danmaku Server.

    :param room_id: the id of live room.
    """
    global is_first
    retry_time = 0
    socket.setdefaulttimeout(TIME_OUT)
    userid = int(100000000 * random.random())
    body = ('{"roomid": ' + str(room_id) + ', "uid": ' + str(userid) +'}')
    while True:
        try:
            sock = socket.socket(socket.AF_INET,
                             socket.SOCK_STREAM)
            address = get_server(room_id)
            sock.connect(
                (address, 788))
            send_data = send_socket_data(sock, 16 + len(body), 16,
                    1, 7, 1, body)
        except socket.error as exc:
            if retry_time == MAX_RETRY:
                if not is_first:
                    terminate()
                raise RuntimeError("重试请求过多,服务中止!")
            print "服务器连接失败..."
            retry_time += 1
            time.sleep(4)
            continue

        if is_first:
            print "开始接收弹幕。(Ctrl + C 退出)"
            is_first = False
        retry_time = 0
        try:
            yield sock
        finally:
            sock.close()