2018-09-27 20:22:35 +03:00
|
|
|
import asyncio
|
2018-07-08 18:45:49 +03:00
|
|
|
|
2018-09-27 20:22:35 +03:00
|
|
|
from .connection import Connection
|
2018-07-08 18:45:49 +03:00
|
|
|
|
|
|
|
|
2018-10-04 18:11:31 +03:00
|
|
|
SSL_PORT = 443
|
2018-09-27 20:22:35 +03:00
|
|
|
|
2018-10-04 18:11:31 +03:00
|
|
|
|
|
|
|
class ConnectionHttp(Connection):
|
|
|
|
async def connect(self, timeout=None, ssl=None):
|
|
|
|
await super().connect(timeout=timeout, ssl=self._port == SSL_PORT)
|
2018-09-27 20:22:35 +03:00
|
|
|
|
|
|
|
def _send(self, message):
|
|
|
|
self._writer.write(
|
2018-07-08 18:45:49 +03:00
|
|
|
'POST /api HTTP/1.1\r\n'
|
2018-09-27 20:22:35 +03:00
|
|
|
'Host: {}:{}\r\n'
|
2018-07-08 18:45:49 +03:00
|
|
|
'Content-Type: application/x-www-form-urlencoded\r\n'
|
|
|
|
'Connection: keep-alive\r\n'
|
|
|
|
'Keep-Alive: timeout=100000, max=10000000\r\n'
|
2018-09-27 20:22:35 +03:00
|
|
|
'Content-Length: {}\r\n\r\n'
|
|
|
|
.format(self._ip, self._port, len(message))
|
2018-07-08 18:45:49 +03:00
|
|
|
.encode('ascii') + message
|
|
|
|
)
|
2018-09-27 20:22:35 +03:00
|
|
|
|
|
|
|
async def _recv(self):
|
|
|
|
while True:
|
|
|
|
line = await self._reader.readline()
|
|
|
|
if not line or line[-1] != b'\n':
|
|
|
|
raise asyncio.IncompleteReadError(line, None)
|
|
|
|
|
|
|
|
if line.lower().startswith(b'content-length: '):
|
|
|
|
await self._reader.readexactly(2)
|
|
|
|
length = int(line[16:-2])
|
|
|
|
return await self._reader.readexactly(length)
|