aiohttp - Python编写异步代理服务器

2017-10-25  本文已影响0人  又小

简介

aiohttpPython3下的一个异步的HTTP库,它可以作为客户端请求数据也可以作为服务器使用。安装很简单。。

pip3 install aiohttp

使用

作为客户端的使用

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import asyncio
from aiohttp import ClientSession

url = "http://www.shuiyueyue.com"
headers = { "User-Agent": "Hi!Pretty!" }

async def darling(loop):
    async with ClientSession(loop=loop) as session:
        async with session.get(url, headers=headers) as resp:
            print(await resp.text())
            
if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(darling(loop))

作为服务器的使用

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import asyncio
from aiohttp import web

async def yoyoyo(request):
    body = "<!DOCTYPE html><html><head><meta charset='utf-8'><title>Yo!Yo!Yo!</title></head><body><p>脑子有壳???</p></body></html>"
    resp = web.Response(body=body.encode("utf-8"))
    resp.content_type= "text/html; charset=utf-8"
    return resp

async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_get("/", yoyoyo)
    return await loop.create_server(app.make_handler(), "", 8080)
    
if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(init(loop))
    loop.run_forever()  

代理

所谓代理,就是相当于帮一对小情侣传纸条的一个中间人的角色,当小花想要将小纸条传给小明的时候,却发现中间站着个老师,这时最好的方法就是让小花把小纸条给老师,然后让老师把小纸条传给小明。同理,当一个客户端想要通信一个连接不到的服务器的时候,简单的方法就是找一个能连接到那个服务器的机子作为代理服务器,过程一般如下:

一个简单的代理服务器所需要处理的就是四个步骤:

实例

废话说了一堆我终于要开始写代码了,首先了解一下请求头和响应头中需要过滤的信息:

知道了要过滤内容之后,可以开始编写处理头部信息的函数了:

bad_headers = ("accept-encoding", "content-encoding", "transfer-encoding", "content-length", "proxy-connection", "connection", "host")

async def fuckheaders(headers):
    h = {}
    for name, value in headers.items():
        if name.lower() not in bad_headers:
            h[name] = value
    h['Connection'] = 'close'
    return h

现在需要编写一个接收客户端请求的小型服务器,aiohttp.web通过middlewares来处理请求或响应的信息,要编写middlewares处理函数需要将处理函数封装到一个函数中,如下:

async def factory(app, handler):
    async def resp_handler(request):
        # 做点啥...也可以不做...取决于你决定什么时候处理信息
        results = await handler(request)
        # 做点啥...也可以不做...取决于你决定什么时候处理信息
        # 如果是作为最后的处理函数最后需要返回响应数据
        response = web.Response(body=results.text)
        return response
    return resp_handler

现在编写这个小型服务器:

async def factory(app, handler):
    async def fuck(request):
        print("==> %s" % request.host)
        method = request.method
        url = str(request.url)
        headers = await fuckheaders(request.headers)
        data = await request.read()
        return await getresp(app.loop, method, url, headers, data)
    return fuck

上面的getresp函数其实就是代理服务器向服务器发送请求的处理函数,现在开始编写:

async def getresp(loop, method, url, headers, data):
    async with ClientSession(loop=loop) as session:
        async with session.request(method, url, headers=headers, data=data) as resp:
            print("<== %s" % resp.host)
            body = await resp.read()
            headers = await fuckheaders(resp.headers)
            response = web.Response(body=body, status=resp.status, reason=resp.reason, headers=headers)
    return reponse

初始化函数:

async def init(loop, port):
    app = web.Application(loop=loop, middlewares=[factory])
    return await loop.create_server(app.make_handler(), "", port)

完成啦,快夸夸自己!几十行代码就能完成一个简单的异步代理服务器,以下是完整代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import asyncio
from aiohttp import web, ClientSession

bad_headers = ("accept-encoding", "content-encoding", "transfer-encoding", "content-length", "proxy-connection", "connection", "host")

async def fuckheaders(headers):
    h = {}
    for name, value in headers.items():
        if name.lower() not in bad_headers:
            h[name] = value
    h['Connection'] = "close"
    return h

async def factory(app, handler):
    async def fuck(request):
        print("==> %s" % request.host)
        method = request.method
        url = str(request.url)
        headers = await fuckheaders(request.headers)
        data = await request.read()
        return await getresp(app.loop, method, url, headers, data)
    return fuck

async def getresp(loop, method, url, headers={}, data={}):
    async with ClientSession(loop=loop) as session:
        async with session.request(method, url, headers=headers, data=data) as resp:
            print("<== %s" % resp.host)
            body = await resp.read()
            headers = await fuckheaders(resp.headers)
            response = web.Response(body=body, status=resp.status, reason=resp.reason, headers=headers)
    return response

async def init(loop, port):
    app = web.Application(loop=loop, middlewares=[factory])
    return await loop.create_server(app.make_handler(), "", port)

def main():
    if len(sys.argv) < 2:
        print("Usage: %s <listen port>" % sys.argv[0])
        sys.exit(1)
    port = int(sys.argv[1])
    loop = asyncio.get_event_loop()
    loop.run_until_complete(init(loop, port))
    loop.run_forever()

if __name__ == "__main__":
    main()

完事儿

GitHub: https://github.com/maoyouxiao/AioProxy
Blog: http://www.shuiyueyue.com

上一篇下一篇

猜你喜欢

热点阅读