简体   繁体   English

使用 Telethon 查找会话 ID 并终止会话

[英]Find session id with telethon and kill the session

Before I ask this question I checked here .在我问这个问题之前,我检查了here I want to kill all other sessions except the session with which I am connecting now.我想终止除我现在连接的会话之外的所有其他会话。 Based on the telethon api I used all_sessions = client(GetAuthorizationsRequest()).to_dict() and I get this result:基于我使用的all_sessions = client(GetAuthorizationsRequest()).to_dict() api all_sessions = client(GetAuthorizationsRequest()).to_dict()我得到了这个结果:

{
       '_': 'Authorization',
       'api_id': ...,
       'app_name': '...',
       'app_version': '4.1.4',
       'country': 'Unknown',
       'date_active': ...,
       'date_created': ...,
       'device_model': 'SamsungSM-G920F',
       'flags': 0,
       'hash': ...,
       'ip': '...',
       'platform': 'Android',
       'region': '',
       'system_version': 'SDK 23'
}

I want to kill this session but I dont know what is te session id mentioned in the linke above(telethon API docs).我想终止此会话,但我不知道上面链接中提到的session id是什么(telethon API 文档)。 I tried with these to commands:我尝试使用这些命令:

client(DestroySessionRequest(api_id))
client(DestroySessionRequest(hash))

But not only no sessions remove but also no response from the apis and the commands waiting and waiting for the response with no error or no exceptions.How can I kill the session?但不仅没有会话删除,而且没有来自 api 的响应和等待响应的命令,没有错误或没有异常。如何终止会话?

To delete current session you: 要删除当前会话,请执行以下操作:

from telethon import TelegramClient

# start session
client = TelegramClient(username, api_id, api_hash).start()

# Now you can use all client methods listed below, like for example...
client.send_message('me', 'Hello to myself!')


# list all sessions
print(client.session.list_sessions())

# delete current session (current session is associated with `username` variable)
client.log_out()

Telethon automatically creates a .session file to store the session details every time a new username is used. Telethon会在每次使用新用户名时自动创建一个.session文件来存储会话详细信息。 The file name starts with the username variable (eg my_username.session ). 文件名以用户名变量开头(例如, my_username.session )。 Session files are stored in file system permanently so you can sometimes see several available sessions. 会话文件永久存储在文件系统中,因此有时您可以看到几个可用的会话。 You can manually remove session files you do not need and the associated session will not be available anymore. 您可以手动删除不需要的会话文件,并且相关的会话将不再可用。 More info about Telethon sessions could be found in Telethon API documentation . 有关Telethon会话的更多信息,请参见Telethon API文档

To kill the other sessions, you need to use the ResetAuthorizationRequest function. 要终止其他会话,您需要使用ResetAuthorizationRequest函数。

Example from official documentation: 官方文档中的示例:

from telethon.sync import TelegramClient
from telethon import functions, types
with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.account.ResetAuthorizationRequest(hash=-12398745604826))
print(result)

https://lonamiwebs.github.io/Telethon/methods/account/reset_authorization.html#examples https://lonamiwebs.github.io/Telethon/methods/account/reset_authorization.html#examples

Try this尝试这个

GetSessions = await client(functions.account.GetAuthorizationsRequest()) 
if len(GetSessions.authorizations)>1:
    print("Another Session    :\tYes")
    for ss in GetSessions.authorizations:
        SessionHash = ss.hash
        SessionIp   = ss.ip
        if SessionHash>0:
            result = await client(functions.account.ResetAuthorizationRequest(hash=SessionHash))
            print("Session Killed     :\t" + str(SessionIp)) 
else:
    print("Another Session    :\tNo")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM