python搭建HTTP/S代理 和 使用HTTP/S代理 - 2

代码示例:

python requests使用代理:

# -*- coding:utf-8 -*-
import requests


if __name__ == "__main__":
    pass
    res = requests.get(
        "http://www.baidu.com/",
        verify=False,
        proxies={
            "http": "host:port",  # 输入代理服务器IP和PORT
            # "https": "ip:port"

        },
        timeout=10
    )
    print(res)

python搭建http/s代理服务器:

# -*- coding:utf-8 -*-
import os
import random
import socket
import _thread
import psutil


class NetInfo(object):

    @staticmethod
    def get_port():
        """
        Gets the free port in the specified range
        :return: Available ports
        """
        net_cmd = "netstat -ntl |grep -v Active| grep -v Proto|awk '{print $4}'|awk -F: '{print $NF}'"
        port_list = os.popen(net_cmd).read().split("\n")
        port = random.randint(1024, 65535)
        if port not in port_list:
            return port
        else:
            NetInfo.get_port()

    @staticmethod
    def get_net_card():
        """Get the network card Ming and IP
        """
        card_list = []
        for card, values in psutil.net_if_addrs().items():
            if card == "lo":
                continue
            address = ""
            for key in values:
                if key.family == 2:
                    address = key.address
            if address == "":
                continue
            card_list.append(card + "-" + address)
        return card_list

    @staticmethod
    def get_local_ips():
        """Get all IP of the machine, not including 127.0.0.1"""
        res = [
            item[1]
            for k, v in psutil.net_if_addrs().items()
            for item in v
            if item[0] == 2
        ]
        if "127.0.0.1" in res:
            res.remove("127.0.0.1")
        return res[0]


class Header:
    """
    用于读取和解析头信息
    """

    def __init__(self, conn):
        self._method = None
        header = b''
        try:
            while 1:
                data = conn.recv(4096)
                header = b"%s%s" % (header, data)
                if header.endswith(b'\r\n\r\n') or (not data):
                    break
        except:
            pass
        self._header = header
        self.header_list = header.split(b'\r\n')
        self._host = None
        self._port = None

    def get_method(self):
        """
        获取请求方式
        :return:
        """
        if self._method is None:
            self._method = self._header[:self._header.index(b' ')]
        return self._method

    def get_host_info(self):
        """
        获取目标主机的ip和端口
        :return:
        """
        if self._host is None:
            method = self.get_method()
            line = self.header_list[0].decode('utf8')
            if method == b"CONNECT":
                host = line.split(' ')[1]
                if ':' in host:
                    host, port = host.split(':')
                else:
                    port = 443
            else:
                for i in self.header_list:
                    if i.startswith(b"Host:"):
                        host = i.split(b" ")
                        if len(host) < 2:
                            continue
                        host = host[1].decode('utf8')
                        break
                else:
                    host = line.split('/')[2]
                if ':' in host:
                    host, port = host.split(':')
                else:
                    port = 80
            self._host = host
            self._port = int(port)
        return self._host, self._port

    @property
    def data(self):
        """
        返回头部数据
        :return:
        """
        return self._header

    def is_ssl(self):
        """
        判断是否为 https协议
        :return:
        """
        if self.get_method() == b'CONNECT':
            return True
        return False

    def __repr__(self):
        return str(self._header.decode("utf8"))


def communicate(sock1, sock2):
    """
    socket之间的数据交换
    :param sock1:
    :param sock2:
    :return:
    """
    try:
        while 1:
            data = sock1.recv(1024)
            if not data:
                return
            sock2.sendall(data)
    except:
        pass


def handle(client):
    """
    处理连接进来的客户端
    :param client:
    :return:
    """
    timeout = 60
    client.settimeout(timeout)
    header = Header(client)
    if not header.data:
        client.close()
        return
    print(header.get_host_info(), header.get_method())
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        server.connect(header.get_host_info())
        server.settimeout(timeout)
        if header.is_ssl():
            data = b"HTTP/1.0 200 Connection Established\r\n\r\n"
            client.sendall(data)
            _thread.start_new_thread(communicate, (client, server))
        else:
            server.sendall(header.data)
        communicate(server, client)
    except:
        server.close()
        client.close()


def serve(ip, port):
    """
    代理服务
    :param ip:
    :param port:
    :return:
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((ip, port))
    s.listen(10)
    print('proxy start...')
    while True:
        conn, addr = s.accept()
        _thread.start_new_thread(handle, (conn,))


if __name__ == '__main__':
    # , NetInfo.get_port()
    IP = NetInfo.get_local_ips()
    PORT = NetInfo.get_port()
    print(IP, PORT)
    serve(IP, PORT)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值