Files
hudozka/utils/Sync/sync/data/request.py
T
2018-11-28 18:37:16 +03:00

54 lines
1.2 KiB
Python

import aiohttp
import requests
import settings
async def upload(url, path):
"""
:param url:
:param path:
:return:
"""
if not settings.upload_enabled:
return url
files = {'file': open(path, 'rb')}
r = requests.put(
url=url,
files=files,
auth=settings.upload_auth
)
if r.status_code == 200:
return url
else:
return None
async def get(url):
async with aiohttp.ClientSession as session:
res = session.get(url)
return await res.text()
async def post(url, data, params={}, headers={}):
async with aiohttp.ClientSession() as session:
async with session.post(url, data=data, params=params, headers=headers) as resp:
return await resp.text()
async def put(url, data):
# login, password = settings.upload_auth
login, password = ('uploader', '207690962638465')
auth = aiohttp.BasicAuth(login, password, encoding='utf-8')
async with aiohttp.request('put', url=url, data=data, auth=auth) as resp:
print(resp.status)
print(await resp.text())
# async with aiohttp.ClientSession(auth=auth) as session:
# res = session.put(url, data=data)
# return await res.text()