使用Python做实现Nginx反向代理

这篇博客展示了如何使用Python编写简易版的Nginx反向代理,解决端口映射和Vue应用部署问题。通过接收请求,解析路径,并根据路径发送文件内容作为响应。
部署运行你感兴趣的模型镜像

以下代码直接双击,便可实现反向代理,可以解决端口映射和vue安装包编译完不能使用的情况:

#/usr/bin/python
###简易版nginx
import socket
import sys
import os

from _thread import *

HOST = '';PORT = 8881
# print(sys.argv);


print(PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket created')

try:
    s.bind((HOST, PORT))
    pass
except Exception:
    # print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
    # print(str(msg[0]))
    sys.exit()

print ('Socket bind complete')

s.listen(10)

print ('Socket now listening')

def assces_log(request):
    # fp = open('http.log', "aw")
    # fp.write(request+"\r\n")
    # fp.close()
    pass

def parse_request(request):
    request = request.splitlines()
    line = request[0]
    line = line.split();
    dict_request = {'method':line[0], 'path':line[1], 'version':line[2]}
    return dict_request

while True:
    conn, addr = s.accept()
    print("----s----")
    print(conn)
    print(addr)
    print("------e-------")
    request = conn.recv(1024)
    print (request)
    bb=str(request)
    print(len(bb))
    if len(bb)>10:
        aaa=bb.split("\\r\\n")
        hosts_port=aaa[1]
        # aaa=bb.split("\\r\\n")
        print(hosts_port)
        hosts_port=hosts_port.split(":")
        host=hosts_port[1]
        port=hosts_port[2]
        print(host)

    # print ("\r\n")
    try:
        dist_request = parse_request(request)
    except Exception as e:
        print(e)
        continue
    path='E:/codes/Kinkajou-shop/Kinkajou/installer/h5'
    print(dist_request['path'])
    filename=bytes.decode(dist_request['path'])
    if filename=='/':
        # dist_request['path']='/index.html'
        filename=r'/index.html'
    path = path + filename
    # print("path="+path)
    # path=path.split('?')[0]
    # print("最终路径:"+path)
    # path=path+str(dist_request['path'], encoding = "UTF-8")

    # print(os.getcwd())
    # path = dist_request['path']
    path = os.getcwd() + filename
    result = eval(repr(path).replace('\\', '/'))
    result = eval(repr(result).replace('//', '/'))
    result=result.split('?')[0]


    if os.path.isfile(result):
        if os.path.exists(result):
            # if path.find('.png') != -1:
            #     fp = open(path, "r")
            # elif path.find('.woff') != -1:
            #     fp = open(path, "r")
            #
            # else:
            #     fp = open(path, "r" ,encoding='UTF-8')
            # reply
            print("最终路径:" + result)
            fp = open(result, "rb",)
            reply = fp.read()

            # reply
            # continue
            response_errno = 200
            response_msg = 'OK'
        else:
            reply = 'Not found page'
            response_errno = 404
            response_msg = 'Not found'
    else:
        reply = 'Forbidden'
        response_errno = 403
        response_msg = 'Forbidden'

    response = 'HTTP/1.1 ' + str(response_errno) + " " + response_msg + '\r\n'
    # response = 'HTTP/1.1 ' + str(response_errno) + " " + response_msg + '\r\n'
    response += "\r\n"
    response=response.encode()
    if isinstance(reply,str):
        reply=reply.encode()
    response += reply
    # print (response)

    # assces_log(request)
    conn.sendall(response)
    # conn.sendall(bytes(response, encoding = "utf8"))
    conn.close()

s.close()

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值