Skip to content

Instantly share code, notes, and snippets.

@alex-eri
Created July 19, 2016 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-eri/10ce6f3c7e6b47413847113606f20ead to your computer and use it in GitHub Desktop.
Save alex-eri/10ce6f3c7e6b47413847113606f20ead to your computer and use it in GitHub Desktop.
$ python3 zte.py
DEBUG:asyncio:Using selector: EpollSelector
Traceback (most recent call last):
File "/usr/lib/python3.5/site-packages/aiohttp/client.py", line 191, in _request
yield from resp.start(conn, read_until_eof)
File "/usr/lib/python3.5/site-packages/aiohttp/client_reqrep.py", line 617, in start
message = yield from httpstream.read()
File "/usr/lib/python3.5/site-packages/aiohttp/streams.py", line 592, in read
result = yield from super().read()
File "/usr/lib/python3.5/site-packages/aiohttp/streams.py", line 447, in read
yield from self._waiter
File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
aiohttp.errors.ServerDisconnectedError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "zte.py", line 82, in <module>
loop.run_until_complete(simple())
File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
return future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
result = coro.throw(exc)
File "zte.py", line 68, in simple
async with await client.get(uri) as c:
File "/usr/lib/python3.5/site-packages/aiohttp/client.py", line 533, in __await__
resp = yield from self._coro
File "/usr/lib/python3.5/site-packages/aiohttp/client.py", line 198, in _request
raise aiohttp.ClientResponseError() from exc
aiohttp.errors.ClientResponseError
ERROR:asyncio:Unclosed client session
client_session: <__main__.Client object at 0x7f25d75027b8>
import logging
import asyncio
import aiohttp
logger = logging.getLogger('zte')
debug = logger.debug
import time
class Client(aiohttp.ClientSession):
def __init__(self,*a,**kw):
self.base_url = kw.pop('url')
self.sms_unread_num = 0
headers = {
'Referer':self.base_url+"/index.html",
'X-Requested-With':'XMLHttpRequest'
}
connector = aiohttp.TCPConnector(force_close=True, conn_timeout=10)
super().__init__(
headers = headers,
version=aiohttp.HttpVersion11,
connector=connector,
*a,**kw)
async def get_count(self,*a,**kw):
uri = "{base}/goform/goform_get_cmd_process?"\
"multi_data=1&isTest=false&sms_received_flag_flag=0&sts_received_flag_flag=0&"\
"cmd=sms_received_flag,sms_unread_num,sms_read_num&_={date}".format(
base=self.base_url,
date=int(time.time()*1000)
)
return self.get(uri,*a,**kw)
async def get_messages(self,*a,**kw):
uri = "{base}/goform/goform_get_cmd_process?"\
"isTest=false&cmd=sms_data_total&page=0&data_per_page=100&mem_store=1&tags=1&"\
"order_by=order+by+id+desc&_={date}".format(
base=self.base_url,
date=int(time.time()*1000)
)
return self.get(uri,*a,**kw)
async def set_msg_read(self,msg_id,*a,**kw):
to_mark = '%3B'.join(msg_id)
uri = "{base}/goform/goform_set_cmd_process"
post = dict(isTest="false",
goformId="SET_MSG_READ",
msg_id=to_mark,
notCallback="true"
)
return self.post(uri,data=post)
async def simple():
client = Client(url='http://192.168.8.1')
uri = "{base}/goform/goform_get_cmd_process?"\
"multi_data=1&isTest=false&"\
"cmd=sms_received_flag,sms_unread_num,sms_read_num&_={date}".format(
base=client.base_url,
date=int(time.time()*1000)
)
async with await client.get(uri) as c:
assert c.status == 200
resp = await c.read()
print(resp[:30])
async with await client.get_count() as c:
assert c.status == 200
resp = await c.read()
print(resp[:30])
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(simple())
finally:
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment