所以,我使用PyTelegramBotAPI在bot的帮助下向我的频道发送消息。 今天我需要为我的机器人设置代理,我阅读了文档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和请求2.18.4
如何通过代理连接到我的代理电报? 这也代表了我的手机和PC的良好工作。
答案 0 :(得分:0)
这里的问题相同。我也需要使用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'
}
答案 1 :(得分:0)
似乎问题出现在您为代理变量分配的字典中。
apihelper.proxy = {'https': 'socks5://login:pass@12.11.22.33:8000'}
为我工作。希望这会有所帮助。
答案 2 :(得分:-1)
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>
好!