Telethon TypeError proxy



我想隐藏我的代理从python代码。所以我试图使用configparser来做到这一点,但我有问题

cpass = configparser.RawConfigParser()
cpass.read('config.data')
try:
api_id = cpass['cred']['id']
api_hash = cpass['cred']['hash']
phone = cpass['cred']['phone']
proxy = cpass['cred']['proxy']
client = TelegramClient(phone, api_id, api_hash, proxy=proxy)

所以当我尝试开始我的会话时,我有错误:TypeError:未知格式的代理:<类'str'>我的代理设置在文件配置中。数据如下:proxy = 'socks5', '185.183.162.152', 8030, True, '82DOLD', 'DTANSF'(这是假代理。不要尝试使用)

如果您需要使用代理访问Telegram,则需要:

  • For Python>= 3.6:安装python-socks[asyncio]
  • For Python<= 3.5:安装PySocks

参数proxy_type允许的值为:

For Python <= 3.5:

  • socks.SOCKS5'socks5'
  • socks.SOCKS4'socks4'
  • socks.HTTP'http'

For Python>= 3.6:

  • 以上全部
  • python_socks.ProxyType.SOCKS5
  • python_socks.ProxyType.SOCKS4
  • python_socks.ProxyType.HTTP

示例>= 3.6

pip3 install "python-socks[asyncio]"
from python_socks import ProxyType
my_proxy = {
'proxy_type': ProxyType.SOCKS5,  # (mandatory) protocol to use (see above)
'addr': '1.1.1.1',               # (mandatory) proxy IP address
'port': 5555,                    # (mandatory) proxy port number
'username': 'foo',               # (optional) username if the proxy requires auth
'password': 'bar',               # (optional) password if the proxy requires auth
'rdns': True                     # (optional) whether to use remote or local resolve, default remote
}
TelegramClient('anon', api_id, api_hash, proxy=my_proxy)

示例<= 3.5

pip3 install PySocks
import socks
my_proxy = (socks.SOCKS5, '1.1.1.1', 5555, True, 'foo', 'bar')
TelegramClient('anon', api_id, api_hash, proxy=my_proxy)

相关内容

  • 没有找到相关文章

最新更新