django - Telethon - 如何设置 Django 项目以在远程服务器上使用 Telethon?
问题描述
我有一个由同事制作的 Django 项目,它运行良好并部署在服务器上。我使用 Telethon 库在单独的测试 Python 项目中测试了方法和新功能(将用户添加到频道、从频道中删除、创建新频道等),它们在我的本地环境中运行良好,只需运行“python file.py" 命令,然后在我的本地环境中使用 Quart 库进行测试。
但是,现在我想将这些功能合并到我的同事在他们的项目中制作的服务器中,以持续运行它,并使用他们需要从 Telethon 获取的来自 Telegram 的数据。
我遇到的第一个错误说Cannot send requests while disconnected
。
以前,我使用 Quart 作为微服务在浏览器上进行调用和请求,但我如何在 Django 服务器上复制此功能?
如果我尝试运行此视图:
async def get_all_channels(request):
try:
dialogs = await client.get_dialogs()
dialogs_str = ''
dialog_list = []
for dialog in dialogs:
dialog_type = 0
if str(type(dialog.entity)) == "<class 'telethon.tl.types.Channel'>":
type_str = 'CANAL'
elif str(type(dialog.entity)) == "<class 'telethon.tl.types.ChannelForbidden'>":
type_str = 'CANAL PROHIBIDO'
elif str(type(dialog.entity)) == "<class 'telethon.tl.types.User'>":
type_str = 'USUARIO'
else:
type_str = str(type(dialog.entity))
if type_str == 'CANAL':
has_admin_rights = dialog.entity.admin_rights is not None
else:
has_admin_rights = False
dialogs_str += '{} - {}\n'.format(dialog.name, str(dialog.id))
if has_admin_rights:
if type_str == 'CANAL':
type_str = 'CANAL CLONADO'
else:
if type_str == 'CANAL':
type_str = 'CANAL ORIGINAL'
if type_str == 'CANAL ORIGINAL':
dialog_type = 1
elif type_str == 'CANAL CLONADO':
dialog_type = 2
elif type_str == 'USUARIO':
dialog_type = 3
dialog_list.append([dialog_type, dialog.name, dialog.id])
dialog_list.sort(key=lambda dialog_list: dialog_list[0])
aux_type = -1
for dialog in dialog_list:
if dialog[0] != aux_type:
if dialog[0] == 0:
print('OTROS:')
elif dialog[0] == 1:
print('\n\nCANALES ORIGINALES:')
elif dialog[0] == 2:
print('\n\nCANALES CLONADOS:')
elif dialog[0] == 3:
print('\n\nUSUARIOS:')
aux_type = dialog[0]
print('{} (ID: {})'.format(dialog[1], str(dialog[2])))
return JsonResponse(json.dumps(dialog_list))
except:
print(traceback.format_exc())
return JsonResponse(traceback.format_exc(), safe=False)
我得到了在线错误dialogs = await client.get_dialogs()
网址很简单,如下所示:
path('get_all_channels/', get_all_channels, name='get_all_channels'),
解决方案
推荐阅读
- php - 如何为 WSDL 服务器做中继?
- wordpress - WordPress 主题开发中的文本域问题
- html - 定义 CSS 网格中的行数
- xaml - XAML MSBuild 任务 XamlMarkupCompilePass1 在并行 MSBuild 中失败并引发错误访问被拒绝
- angular - 如何在同一个项目中使用另一个库的角度构建一个库
- c++ - 如何评估开关中的 QChar 对象?
- python - ImportError:无法从部分初始化的模块“gi”导入名称“_gi”(很可能是由于循环导入)
- rest - 弹性搜索/Logstash
- isabelle - Simp 不使用 Isabelle 中提供的引理
- mysql - SQL在连接中选择最近的行