E
E
Evgeny Dolbanovsky2019-02-03 23:43:57
Python
Evgeny Dolbanovsky, 2019-02-03 23:43:57

Pycharm 2018.3.3 script not working via RUN but working via DEBUG?

There is a simple bot script on LongPollApi for the VK community. I can’t understand why the script gives a connection error via RUN, and if you run it via DEBUG, then a connection occurs to the LongPollApi server - and the bot responds to the message.
Something I do not quite understand ... How so ???
bot code:

import vk
from config import *
from requests import *
import os
import random

session = vk.Session(access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
api = vk.API(session, v = '5.92')

# Первый запрос к LongPoll: получаем server и key
longPoll = api.groups.getLongPollServer(group_id = 'XXXXXXXXXXX')
server, key, ts = longPoll['server'], longPoll['key'], longPoll['ts']

while True:
    # Последующие запросы: меняется только ts
    longPoll = post('%s'%server, data = {'act': 'a_check',
                                         'key': key,
                                         'ts': ts,
                                         'wait': 25}).json()


    if longPoll['updates'] and len(longPoll['updates']) != 0:

        print(longPoll)

        for update in longPoll['updates']:
            if update['type'] == 'message_new':
                # Помечаем сообщение от этого пользователя как прочитанное
                mess_new = api.messages.markAsRead(peer_id = update['object']['from_id'])

                # Запрашиваем имя пользователя
                name_user = api.users.get(user_ids = update['object']['from_id'])[0]['first_name']
                print('Отправил боту', name_user, '| прочитано-',mess_new)


                # Отправляем сообщение "Привет друг"
                mess_text = api.messages.send(user_id = update['object']['from_id'], message = 'Привет друг', random_id = [random.getrandbits(64)])



    # Меняем ts для следующего запроса
    ts = longPoll['ts']

Here is the error code for running the script via RUN:
C:\CODE\Python\loli-bots-group\venv\Scripts\python.exe C:/CODE/Python/loli-bots-group/loli_main.py
Traceback (most recent call last):
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection
    raise err
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection
    sock.connect(sa)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\connection.py", line 301, in connect
    conn = self._new_conn()
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\connection.py", line 164, in _new_conn
    (self.host, self.timeout))
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.VerifiedHTTPSConnection object at 0x000001F05D55FA58>, 'Connection to api.vk.com timed out. (connect timeout=10)')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\urllib3\util\retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.vk.com', port=443): Max retries exceeded with url: /method/groups.getLongPollServer (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x000001F05D55FA58>, 'Connection to api.vk.com timed out. (connect timeout=10)'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/CODE/Python/loli-bots-group/loli_main.py", line 11, in <module>
    longPoll = api.groups.getLongPollServer(group_id = '177546530')
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\vk\api.py", line 173, in __call__
    return self._api._session.make_request(self)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\vk\api.py", line 67, in make_request
    response = self.send_api_request(method_request, captcha_response=captcha_response)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\vk\api.py", line 115, in send_api_request
    response = self.requests_session.post(url, method_args, timeout=timeout)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\requests\sessions.py", line 581, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\vk\utils.py", line 78, in request
    response = super(LoggingSession, self).request(method, url, **kwargs)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\CODE\Python\loli-bots-group\venv\lib\site-packages\requests\adapters.py", line 504, in send
    raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='api.vk.com', port=443): Max retries exceeded with url: /method/groups.getLongPollServer (Caused by ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection object at 0x000001F05D55FA58>, 'Connection to api.vk.com timed out. (connect timeout=10)'))

Process finished with exit code 1

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question