mathsman5133 / coc.py

Python API Wrapper for Clash of Clans.
https://cocpy.readthedocs.io/en/latest/
MIT License
121 stars 42 forks source link

[Errno 104] Connection reset by peer Unclosed client session #19

Closed Anatras closed 5 years ago

Anatras commented 5 years ago

I'm getting this everytime on linux (debian 9)

aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer
Unclosed client session
  File "/root/LootPvp/env/lib/python3.7/site-packages/aiohttp/client.py", line 497, in _request
    await resp.start(conn)
  File "/root/LootPvp/env/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 844, in start
    message, payload = await self._protocol.read()  # type: ignore  # noqa
  File "/root/LootPvp/env/lib/python3.7/site-packages/aiohttp/streams.py", line 588, in read
    await self._waiter
  File "/usr/local/lib/python3.7/asyncio/futures.py", line 260, in __await__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 292, in __wakeup
    future.result()
  File "/usr/local/lib/python3.7/asyncio/futures.py", line 178, in result
    raise self._exception
aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fd9cd8b9470>
Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7fd9cd77ae88>, 2247353.651511028)]']
connector: <aiohttp.connector.TCPConnector object at 0x7fd9cd8b94a8>
mathsman5133 commented 5 years ago

What is the relevant code?

Anatras commented 5 years ago
import coc
import asyncio

def setup(client):
    client.edit_cache('players', 1024, 300)
    return 

client = coc.login('email@email.com', 'password', default_cache=setup)

async def get_some_player(tag):
    player = await client.get_player(tag)

    print(player.name)
    # alternatively,
    print(str(player))

async def get_five_clans(name):
    players = await client.search_clans(name=name, limit=5)
    for n in players:
        print(n, n.tag)

async def main():
    await get_some_player('#9RYU9PUV2')
    await get_five_clans('guran turino')

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
Anatras commented 5 years ago

It works fine on Windows

Anatras commented 5 years ago

And the problem is caused by the login, because if I remove it the error does not show up, but obviously the code does not work

mathsman5133 commented 5 years ago

Could you please re-run the code, enable logging and share the results?

import logging
logging.basicConfig(level=logging.DEBUG)

should do the trick

Anatras commented 5 years ago

I don't know why lol

Traceback (most recent call last): File "prova.py", line 4, in logging.setLevel(logging.DEBUG) AttributeError: module 'logging' has no attribute 'setLevel'

mathsman5133 commented 5 years ago

Sorry I edited it like 30 seconds after, lol. Could you recopy/paste?

Anatras commented 5 years ago

It's returning the same error as the beggining

mathsman5133 commented 5 years ago

There is no extra lines of stdout?

Anatras commented 5 years ago

what is it? I cannot take the full traceback because debian 9 cuts it

Anatras commented 5 years ago
2M2Y2NhNSJ9.eyJqdGkiOiJkNjk1MTUzNC1iYzFjLWYxNGUtZGY3Yi03ZDg1YTQ1NjMxNGIiLCJzdWIiOiIzZWIxM2VmMi1jM2ZlLWYwNWMtZDFkMy00ODhhNDI2NTYwNjkiLCJleHAiOjE1NjQwNDE4ODIsImdhbWUiOiJjbGFzaCIsInJvbGUiOiJkZXZlbG9wZXIifQ.BP8nFEhRM-1xoTDmZqTLgrgtWtZ0Ky1Uup8xZkeaIEr7z5umm2JGwGVrblerugLIs8a_2SIoUbcYaIw2SkLBEA', 'ua': None, 'ip': None}, 'developer': {'id': '3eb13ef2-c3fe-f05c-d1d3-488a42656069', 'name': 'Anatras02', 'game': 'clash', 'email': 'balderialessio@gmail.com', 'tier': 'developer/silver', 'allowedScopes': None, 'maxCidrs': None, 'prevLoginTs': '2019-07-25T07:04:33.062Z', 'prevLoginIp': '185.25.207.93', 'prevLoginUa': 'Amazon CloudFront'}, 'temporaryAPIToken': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6IjIxN2EyNTg5LWIyYWQtNjZhOS1lN2E3LWJkY2U4Y2QxYzhhMCIsImlhdCI6MTU2NDAzODI4MiwiZXhwIjoxNTY0MDQxODgyLCJzdWIiOiJkZXZlbG9wZXIvM2ViMTNlZjItYzNmZS1mMDVjLWQxZDMtNDg4YTQyNjU2MDY5Iiwic2NvcGVzIjpbImNsYXNoIl0sImxpbWl0cyI6W3sidGllciI6ImRldmVsb3Blci9icm9uemUiLCJ0eXBlIjoidGhyb3R0bGluZyJ9LHsiY2lkcnMiOlsiMTg1LjI1LjIwNy45My8zMiJdLCJ0eXBlIjoiY2xpZW50In0seyJvcmlnaW5zIjpbImRldmVsb3Blci5jbGFzaG9mY2xhbnMuY29tIl0sInR5cGUiOiJjb3JzIn1dfQ.C139K_MLNjXoBYLkG7T59474beXXXGFQZ5cduTQGLzHFc4tbNCYyZCS_6R-zwaefaiOCo9S9iu51UhM6QYhjFA', 'swaggerUrl': 'https://api.clashofclans.com/v1/'}
DEBUG:coc.http:https://developer.clashofclans.com/api/apikey/list has received {'status': {'code': 0, 'message': 'ok', 'detail': None}, 'sessionExpiresInSeconds': 3599, 'keys': [{'id': '0da2766a-16e8-4206-b37d-caae3356e5ca', 'developerId': '3eb13ef2-c3fe-f05c-d1d3-488a42656069', 'tier': 'developer/silver', 'name': 'Created with coc.py Client', 'description': 'Created on Thu Jul 25 03:38:38 2019', 'origins': None, 'scopes': ['clash'], 'cidrRanges': ['87.3.200.175'], 'validUntil': None, 'key': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6IjBkYTI3NjZhLTE2ZTgtNDIwNi1iMzdkLWNhYWUzMzU2ZTVjYSIsImlhdCI6MTU2NDAxODcxOSwic3ViIjoiZGV2ZWxvcGVyLzNlYjEzZWYyLWMzZmUtZjA1Yy1kMWQzLTQ4OGE0MjY1NjA2OSIsInNjb3BlcyI6WyJjbGFzaCJdLCJsaW1pdHMiOlt7InRpZXIiOiJkZXZlbG9wZXIvc2lsdmVyIiwidHlwZSI6InRocm90dGxpbmcifSx7ImNpZHJzIjpbIjg3LjMuMjAwLjE3NSJdLCJ0eXBlIjoiY2xpZW50In1dfQ.RkPGi1BFT93K3tIr3yj-Fwh4tnQvmoeuRl8WohaJMICWqKqSHQ2kKDc2-kX58uqjXyFSnCAIklyGFqKY5rdjYQ'}, {'id': '8aca459b-0955-4937-9692-49f8aa9f4358', 'developerId': '3eb13ef2-c3fe-f05c-d1d3-488a42656069', 'tier': 'developer/silver', 'name': 'TheFamilyStats_Bot', 'description': 'Bot Telegram', 'origins': None, 'scopes': ['clash'], 'cidrRanges': ['149.154.167.99'], 'validUntil': None, 'key': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6IjI4YTMxOGY3LTAwMDAtYTFlYi03ZmExLTJjNzQzM2M2Y2NhNSJ9.eyJpc3MiOiJzdXBlcmNlbGwiLCJhdWQiOiJzdXBlcmNlbGw6Z2FtZWFwaSIsImp0aSI6IjhhY2E0NTliLTA5NTUtNDkzNy05NjkyLTQ5ZjhhYTlmNDM1OCIsImlhdCI6MTU2NDAxMDgxOCwic3ViIjoiZGV2ZWxvcGVyLzNlYjEzZWYyLWMzZmUtZjA1Yy1kMWQzLTQ4OGE0MjY1NjA2OSIsInNjb3BlcyI6WyJjbGFzaCJdLCJsaW1pdHMiOlt7InRpZXIiOiJkZXZlbG9wZXIvc2lsdmVyIiwidHlwZSI6InRocm90dGxpbmcifSx7ImNpZHJzIjpbIjE0OS4xNTQuMTY3Ljk5Il0sInR5cGUiOiJjbGllbnQifV19.yttXchZcSTELeFBlMlYQBZ4AXFkH8RoXGapgWE92zPedHFJtjXL5RP3q_PAkCuOJlxz1UkPGi6BnYi97rXPz7g'}]}
Traceback (most recent call last):
  File "prova.py", line 38, in <module>
    loop.run_until_complete(main())
  File "/usr/local/lib/python3.7/site-packages/coc/nest_asyncio.py", line 63, in run_until_complete
    return self._run_until_complete_orig(future)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.7/asyncio/futures.py", line 178, in result
    raise self._exception
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 223, in __step
    result = coro.send(None)
  File "prova.py", line 33, in main
    await get_some_player('#9RYU9PUV2')
  File "prova.py", line 21, in get_some_player
    player = await client.get_player(tag)
NameError: name 'client' is not defined
ERROR:asyncio:Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fad4ad0aac8>
ERROR:asyncio:Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7fad4ab88108>, 18588.392184461)]']
connector: <aiohttp.connector.TCPConnector object at 0x7fad4ad0ab00>
mathsman5133 commented 5 years ago

Ok. Could you try removing the client = coc.login(...) line and replacing if __name__=='__main__': with the following please?

if __name__ == '__main__':
    client = coc.login('email@email.com', 'password', default_cache=setup)
    client.loop.run_until_complete(main())
Anatras commented 5 years ago

Nothing changed

mathsman5133 commented 5 years ago

what about a new/blank file with

import coc
import logging
logging.basicConfig(level=logging.DEBUG)

client = coc.login('email@email.com', 'password')
print(client)

could you please copy/paste all logs?

Anatras commented 5 years ago
Traceback (most recent call last):
  File "prova2.py", line 5, in <module>
    client = coc.login('email', 'password')
  File "/usr/local/lib/python3.7/site-packages/coc/client.py", line 98, in login
    c.loop.run_until_complete(c.login(email, password))
  File "/usr/local/lib/python3.7/site-packages/coc/nest_asyncio.py", line 63, in run_until_complete
    return self._run_until_complete_orig(future)
  File "/usr/local/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "/usr/local/lib/python3.7/asyncio/futures.py", line 178, in result
    raise self._exception
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 225, in __step
    result = coro.throw(exc)
  File "/usr/local/lib/python3.7/site-packages/coc/client.py", line 229, in login
    await self.http.get_keys()
  File "/usr/local/lib/python3.7/site-packages/coc/http.py", line 141, in get_keys
    ip = await self.get_ip()
  File "/usr/local/lib/python3.7/site-packages/coc/http.py", line 222, in get_ip
    async with self.__session.request('GET', 'http://ip.42.pl/short') as r:
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 1005, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 497, in _request
    await resp.start(conn)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client_reqrep.py", line 844, in start
    message, payload = await self._protocol.read()  # type: ignore  # noqa
  File "/usr/local/lib/python3.7/site-packages/aiohttp/streams.py", line 588, in read
    await self._waiter
  File "/usr/local/lib/python3.7/asyncio/futures.py", line 260, in __await__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 292, in __wakeup
    future.result()
  File "/usr/local/lib/python3.7/asyncio/futures.py", line 178, in result
    raise self._exception
aiohttp.client_exceptions.ClientOSError: [Errno 104] Connection reset by peer
ERROR:asyncio:Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fd9b8579828>
ERROR:asyncio:Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x7fd9b83f5108>, 19961.921803089)]']
connector: <aiohttp.connector.TCPConnector object at 0x7fd9b8579860>
mathsman5133 commented 5 years ago

Can you otherwise access the internet/make web requests?

Anatras commented 5 years ago

I'm connected to internet, is it possible that maybe the ip in the CoC api is wrong? But it's working fine on windows

mathsman5133 commented 5 years ago

I don't think so - it can't even get your IP address from http://ip.42.pl/short. What happens if you run this (straight from aiohttp docs, not related to coc.py)?

import aiohttp
import asyncio

async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('http://python.org') as response:
            print(await response.text())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Anatras commented 5 years ago

that works

Anatras commented 5 years ago

so what do you thing is the problem?

mathsman5133 commented 5 years ago

I'm not sure. Do you have any firewalls? What happens if you replace https://python.org with http://ip.42.pl/short?

Anatras commented 5 years ago

with the second url it gives me the error

mathsman5133 commented 5 years ago

Cool. What about this one? https://api.ipify.org/

Anatras commented 5 years ago

it prints 185.25.207.93

mathsman5133 commented 5 years ago

Wonderful. I'll make a commit, get you to pip install git+https://github.com/mathsman5133/coc.py --upgrade again, and hopefully, it works.

Anatras commented 5 years ago

Yes, it's working thank you so much!!!!!