PyTelegramBotAPI 错误连接通过代理

PyTelegramBotAPI error connection through Proxy

所以,我使用 PyTelegramBotAPI 在机器人的帮助下将消息发送到我的频道。 今天我需要为我的机器人设置代理,我阅读了文档 https://github.com/eternnoir/pyTelegramBotAPI#proxy

这样做:

import telebot
from telebot import apihelper

bot = telebot.TeleBot(config.telebot_token)
apihelper.proxy = {'https', 'socks5://login:pass@12.11.22.33:8000'}

并得到异常:

Connected to pydev debugger (build 172.3757.67)
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1023, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/Stas/PycharmProjects/newsbot/parser.py", line 658, in <module>
    RssProcess('rsslinks.txt')
  File "/Users/Stas/PycharmProjects/newsbot/parser.py", line 511, in __init__
    telegrambot.bot.send_message(config.telebot_id, 'RssProcess Started...')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telebot/__init__.py", line 439, in send_message
    reply_markup, parse_mode, disable_notification))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telebot/apihelper.py", line 135, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/telebot/apihelper.py", line 54, in _make_request
    timeout=(connect_timeout, read_timeout), proxies=proxy)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 499, in request
    prep.url, proxies, stream, verify, cert
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 671, in merge_environment_settings
    no_proxy = proxies.get('no_proxy') if proxies is not None else None
AttributeError: 'set' object has no attribute 'get'

我尝试阅读错误的地方并发现了这样的代码:

if self.trust_env:
    # Set environment's proxies.
    no_proxy = proxies.get('no_proxy') if proxies is not None else None
    env_proxies = get_environ_proxies(url, no_proxy=no_proxy)
    for (k, v) in env_proxies.items():
        proxies.setdefault(k, v)

    # Look for requests environment configuration and be compatible
    # with cURL.
    if verify is True or verify is None:
        verify = (os.environ.get('REQUESTS_CA_BUNDLE') or
                  os.environ.get('CURL_CA_BUNDLE'))

行中的主要问题

no_proxy = proxies.get('no_proxy')

我使用 PyCharm,安装

PySocks 1.6.8 and Requests 2.18.4

如何通过代理连接到我的代理电报? 该代理在我的 phone 和 PC 上也很好用。

同样的问题。我也需要使用 http 和 https,这段代码对我有用:

import telebot
from telebot import apihelper

bot = telebot.TeleBot(config.telebot_token)
apihelper.proxy = {
  'http', 'socks5://login:pass@12.11.22.33:8000',
  'https', 'socks5://login:pass@12.11.22.33:8000'
}

看来问题出在您分配给代理变量的字典中。

apihelper.proxy = {'https': 'socks5://login:pass@12.11.22.33:8000'}

对我有用。希望这有帮助。

pip3 uninstall requests
pip3 uninstall requests==2.10.0
#socks5 Tor (no login, no pass)
apihelper.proxy = {'https':'socks5://127.0.0.1:9150'}
#or soks5
apihelper.proxy = {'https':'socks5://login:pass@orbtl.s5.opennetwork.cc:port'}

apihelper.proxy = {'https':'socks5://14611055481:U777Vluhz8@orbtl.s5.opennetwork.cc:999'}<br>

好的!