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

简介

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 = "Yo!Yo!Yo!

脑子有壳???

" 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()

代理

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

  • 客户端发送请求
  • 代理服务器接收来自客户端的请求
  • 代理服务器将客户端的请求发送给服务器
  • 服务器接收来自代理服务器的请求并响应
  • 代理服务器接收来自服务器的响应
  • 代理服务器将响应发送回客户端
  • 客户端接收响应

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

  • 接收客户端请求
  • 发送客户端请求至服务器
  • 接收服务器响应
  • 发送服务器响应至客户端

实例

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

  • Content-Encoding ,Accept-Encoding 这两个交给代理服务器自己去处理,省得出现什么压缩错误
  • Transfer-Encoding 去掉分块传输的响应头,由代理服务器自身处理
  • Content-Length 内容字节数也交给服务器处理,否则如果修改了内容会导致长度不一
  • Proxy-Connection 这个是使用代理服务器才会出现的,恩,怎么能让人知道你在使用代理服务器呢
  • Connection 替换掉Proxy-Connection并将值修改为close,否则如果使用keep-alive可能会导致分块传输
  • Host 尝试过几次因为没有去掉这个导致无法连接,所以让代理服务器自己处理这个就好了

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

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 " % 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

你可能感兴趣的:(aiohttp - Python编写异步代理服务器)