python项目源码解析1:blivedm
源码:blivedm
Python获取bilibili直播弹幕的库,使用WebSocket协议,支持web端和B站直播开放平台两种接
本文能帮助 解决 一个问题
。
SESSDATA 是 B站 的登陆凭证,每个账户登录后都会在服务器生成一个单独的 SESSDATA, 如果不刷新有效期为半个月。
可以用 SESSDATA 来请求某些必须要登录状态才能访问的 A
-
chrome浏览器打开bilibili 并登录
-
f12
-
点开下拉菜单依次选择application -> cookies -> www.bilibili.com
-
storage
-
cookeis

剩余是代码学习
def set_handler(self, handler: Optional['handlers.HandlerInterface']):
"""
设置消息处理器
注意消息处理器和网络协程运行在同一个协程,如果处理消息耗时太长会阻塞接收消息。如果是CPU密集型的任务,建议将消息推到线程池处理;
如果是IO密集型的任务,应该使用async函数,并且在handler里使用create_task创建新的协程
:param handler: 消息处理器
"""
self._handler = handler
pyhon Optional
typing.Optional 可选类型
Optional[X]等价于 Union[X, None]
How to Run an Asyncio Program in Python
-
asyncio is a library to write concurrent code using the async/await syntax.
asyncio is used as a foundation for multiple Python asynchronous frameworks that provide high-performance network and web-servers, database connection libraries, distributed task queues, etc.
asyncio is often a perfect fit for IO-bound and high-level structured network code.
-
The run() function will start the asyncio event loop and will schedule the coroutine provided as an argument. This function runs the passed coroutine, taking care of managing the asyncio event loop, finalizing asynchronous generators, and closing the threadpool.
# SuperFastPython.com
# example of starting an asyncio program
import asyncio
# custom coroutine
async def custom_coro(message):
# report the message
print(message)
# create and execute coroutine
asyncio.run(custom_coro('hello world'))
本文由 mdnice 多平台发布