Python代码实现http/https代理服务器的脚本


Posted in Python onAugust 12, 2019

一个几百行代码做出http/https代理服务器的脚本,启动即可做http https透明代理使用

python proxy.py 8992

使用非阻塞io模式,性能还可以。

可以和浏览器一样保持长连接,代码有点乱,不管那么多了能跑就行

几百行代码做出http/https代理服务器代码片段

*1. * [代码] [Python]代码

#!/usr/bin/python
#-*- coding:utf-8 -*-
import socket, logging
import select, errno
import os
import sys
import traceback
import gzip
from StringIO import StringIO
import Queue
import threading
import time
import thread
import cgi
from cgi import parse_qs
import json
import imp
from os.path import join, getsize
import re
import ssl
##################user config ##################
logger = logging.getLogger("network-server")
#############################################
def getTraceStackMsg():
 tb = sys.exc_info()[2]
 msg = ''
 for i in traceback.format_tb(tb):
  msg += i
 return msg
def InitLog():
 logger.setLevel(logging.DEBUG)
 fh = logging.FileHandler("network-server.log")
 fh.setLevel(logging.DEBUG)
 ch = logging.StreamHandler()
 ch.setLevel(logging.ERROR)
 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
 ch.setFormatter(formatter)
 fh.setFormatter(formatter)
 logger.addHandler(fh)
 logger.addHandler(ch)
def clearfdpro(epoll_fd, params, fd):
 try:
  fd_check = int(fd)
 except Exception, e:
  print "fd error"
  sys.exit(1)
 try:
  #print "pid:%s, close fd:%s" % (os.getpid(), fd)
  epoll_fd.unregister(fd)
 except Exception, e:
  #print str(e)+getTraceStackMsg()
  pass
 try:
  param = params[fd]
  try:
   addr = param["addr"]
   if "next" in param:
    print "close sock, %s:%s" % (addr[0], addr[1])
  except Exception, e:
   pass
  param["connections"].shutdown(socket.SHUT_RDWR)
  param["connections"].close()
  f = param.get("f", None)
  if f != None:
   f.close()
  rc = param.get("rc", None)
  if rc != None:
   rc.close()
  if "read_cache_name" in param:
   os.remove(param["read_cache_name"])
 except Exception, e:
  #print str(e)+getTraceStackMsg()
  pass
 try:
  del params[fd]
  #logger.error(getTraceStackMsg())
  #logger.error("clear fd:%s" % fd)
 except Exception, e:
  #print str(e)+getTraceStackMsg()
  pass
def clearfd(epoll_fd, params, fd):
 try:
  param = params[fd]
  if "nextfd" in param:
   nextfd = param["nextfd"]
   next_param = params[nextfd]
   del param["nextfd"]
   del next_param["nextfd"]
   if not "next" in param: #masterfd
    clearfdpro(epoll_fd, params, nextfd)
   else: # nextfd
    if not "writedata" in next_param or len(next_param["writedata"]) == 0:
     clearfdpro(epoll_fd, params, nextfd)
    else:
     next_param["sendandclose"] = "true"
  clearfdpro(epoll_fd, params, fd)
 except Exception, e:
  #print str(e)+getTraceStackMsg()
  pass
def FindHostPort(datas):
 host_s = -1
 host_e = -1
 host_str = None
 host = ""
 port = ""
 if not datas.startswith("CONNECT"):
  host_s = datas.find("Host:")
  if host_s < 0:
   host_s = datas.find("host:")
  if host_s > 0:
   host_e = datas.find("\r\n", host_s)
  if host_s > 0 and host_e > 0:
   host_str = datas[host_s+5:host_e].strip()
   add_list = host_str.split(":")
   if len(add_list) == 2:
    host = add_list[0]
    port = add_list[1]
   else:
    host = add_list[0]
    port = 80
   first_seg = datas.find("\r\n")
   first_line = datas[0:first_seg]
   first_line = first_line.replace(" http://%s" % host_str, " ")
   datas = first_line + datas[first_seg:]
 else:
  first_seg = datas.find("\r\n")
  head_e = datas.find("\r\n\r\n")
  if first_seg > 0 and head_e > 0:
   first_line = datas[0:first_seg]
36a0
   com,host_str,http_version = re.split('\s+', first_line)
   add_list = host_str.split(":")
   if len(add_list) == 2:
    host = add_list[0]
    port = add_list[1]
   else:
    host = add_list[0]
    port = 443
   host_s = 1
   host_e = 1
 return host_str,host_s,host_e,host,port,datas
def connect_pro(params, param, epoll_fd, datas, fd, cur_time, host, port):
 try:
  nextfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
  nextfd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  nextfd.settimeout(5)
  try:
   nextfd.connect((host, int(port)))
  except Exception, e:
   print "########%s,%s connect fail" % (host,port)
  nextfd.setblocking(0)
  next_fileno = nextfd.fileno()
  print "pid:%s, connect %s:%s fd:%s" % (os.getpid(), host, port, next_fileno)
  if next_fileno in params:
   print "fileno exist"
   sys.exit(1)
  if not datas.startswith("CONNECT"):
   next_param = {"addr":[host,port],"writelen":0, "connections":nextfd, "time":cur_time, "nextfd":fd}
   param["nextfd"] = next_fileno
   next_param["writedata"] = datas
   next_param["writelen"] = 0
   next_param["next"] = "true"
   param["read_len"] = 0
   param["readdata"] = ""
   params[next_fileno] = next_param
   epoll_fd.register(next_fileno, select.EPOLLIN | select.EPOLLOUT | select.EPOLLERR | select.EPOLLHUP)
   epoll_fd.modify(fd, select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)
  else:
   next_param = {"addr":[host,port],"writelen":0, "connections":nextfd, "time":cur_time, "nextfd":fd}
   param["nextfd"] = next_fileno
   next_param["next"] = "true"
   params[next_fileno] = next_param
   epoll_fd.register(next_fileno, select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)
   param["read_len"] = 0
   param["readdata"] = ""
   param["writedata"] = "HTTP/1.1 200 Connection Established\r\nConnection: close\r\n\r\n"
   param["writelen"] = 0
   param["reuse"] = "true"
   epoll_fd.modify(fd, select.EPOLLIN | select.EPOLLOUT | select.EPOLLERR | select.EPOLLHUP)
 except socket.error, msg:
  clearfd(epoll_fd,params,fd)
def process_datas(process_status, params, param, epoll_fd, datas, read_len, fd, cur_time):
 if process_status == "close":
  clearfd(epoll_fd,params,fd)
 else:
  need_connect = False
  host_str = None
  host_s = -1
  host_e = -1
  if "reuse" in param and "next" not in param:
   if not datas.startswith("CONNECT") and \
     not datas.startswith("GET") and \
     not datas.startswith("POST") and \
     not datas.startswith("PUT"):
    del param["reuse"]
   else:
    host_str,host_s,host_e,host,port,datas = FindHostPort(datas)
    host_s = int(host_s)
    host_e = int(host_e)
    next_fileno = param["nextfd"]
    next_param = params[next_fileno]
    addr = next_param["addr"]
    if host_s > 0 and host_e > 0:
     if host != addr[0] or str(port) != str(addr[1]):
      print "%s,%s neq %s,%s" % (host,port,addr[0],addr[1])
      need_connect = True
      del param["nextfd"]
      del next_param["nextfd"]
      clearfd(epoll_fd,params,next_fileno)
     del param["reuse"]
    else:
     param["read_len"] = read_len
     param["readdata"] = datas
     return None
  if need_connect or not "nextfd" in param:
   if host_str == None or not host_s > 0 or not host_e > 0:
    host_str,host_s,host_e,host,port,datas = FindHostPort(datas)
    host_s = int(host_s)
    host_e = int(host_e)
   if host_s > 0 and host_e > 0:
    if not datas.startswith("CONNECT"):
     epoll_fd.modify(fd, select.EPOLLERR | select.EPOLLHUP) # 简单处理,http连接时把读去掉,避免内存攻击
    thread.start_new_thread(connect_pro,(params, param, epoll_fd, datas, fd, cur_time, host, port))
   else:
    param["read_len"] = read_len
    param["readdata"] = datas
  else:
   next_fileno = param["nextfd"]
   next_param = params[next_fileno]
   if "next" in param:
    next_param["reuse"] = "true"
   write_data = next_param.get("writedata", "")
   write_data += datas
   next_param["writedata"] = write_data
   param["read_len"] = 0
   param["readdata"] = ""
   epoll_fd.modify(next_fileno, select.EPOLLIN | select.EPOLLOUT | select.EPOLLERR | select.EPOLLHUP)
  if process_status == "close_after_process":
   print "close after process"
   clearfd(epoll_fd,params,fd)
def run_main(listen_fd):
 try:
  epoll_fd = select.epoll()
  epoll_fd.register(listen_fd.fileno(), select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)
  print "listen_fd:%s" % listen_fd.fileno()
 except select.error, msg:
  logger.error(msg)
 params = {}
 last_min_time = -1
 while True:
  epoll_list = epoll_fd.poll()
  cur_time = time.time()
  for fd, events in epoll_list:
   if fd == listen_fd.fileno():
    while True:
     try:
      conn, addr = listen_fd.accept()
      conn.setblocking(0)
      epoll_fd.register(conn.fileno(), select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)
      conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      #conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
      params[conn.fileno()] = {"addr":addr,"writelen":0, "connections":conn, "time":cur_time}
     except socket.error, msg:
      break
   elif select.EPOLLIN & events:
    param = params.get(fd,None)
    if param == None:
     continue
    param["time"] = cur_time
    datas = param.get("readdata","")
    cur_sock = params[fd]["connections"]
    read_len = param.get("read_len", 0)
    process_status = "close"
    while True:
     try:
      data = cur_sock.recv(102400)
      if not data:
       if datas == "":
        break
       else:
        raise Exception("close after process")
      else:
       datas += data
       read_len += len(data)
     except socket.error, msg:
      if msg.errno == errno.EAGAIN:
       process_status = "process"
       break
      else:
       break
     except Exception, e:
      process_status = "close_after_process"
      break
    process_datas(process_status, params, param, epoll_fd, datas, read_len, fd, cur_time)
   elif select.EPOLLHUP & events or select.EPOLLERR & events:
    clearfd(epoll_fd,params,fd)
    logger.error("sock: %s error" % fd)
   elif select.EPOLLOUT & events:
    param = params.get(fd,None)
    if param == None:
     continue
    param["time"] = cur_time
    sendLen = param.get("writelen",0)
    writedata = param.get("writedata", "")
    total_write_len = len(writedata)
    cur_sock = param["connections"]
    f = param.get("f", None)
    totalsenlen = param.get("totalsenlen", None)
    if writedata == "":
     clearfd(epoll_fd,params,fd)
     continue
    while True:
     try:
      sendLen += cur_sock.send(writedata[sendLen:])
      if sendLen == total_write_len:
       if f != None and totalsenlen != None:
        readmorelen = 102400
        if readmorelen > totalsenlen:
         readmorelen = totalsenlen
        morefiledata = ""
        if readmorelen > 0:
         morefiledata = f.read(readmorelen)
        if morefiledata != "":
         writedata = morefiledata
         sendLen = 0
         total_write_len = len(writedata)
         totalsenlen -= total_write_len
         param["writedata"] = writedata
         param["totalsenlen"] = totalsenlen
         continue
        else:
         f.close()
         del param["f"]
         del param["totalsenlen"]
       if not "sendandclose" in param:
        param["writedata"] = ""
        param["writelen"] = 0
        epoll_fd.modify(fd, select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)
       else:
        clearfd(epoll_fd,params,fd)
       break
     except socket.error, msg:
      if msg.errno == errno.EAGAIN:
       param["writelen"] = sendLen
       break
      clearfd(epoll_fd,params,fd)
      break
   else:
    continue
  #check time out
  if cur_time - last_min_time > 20:
   last_min_time = cur_time
   objs = params.items()
   for (key_fd,value) in objs:
    fd_time = value.get("time", 0)
    del_time = cur_time - fd_time
    if del_time > 20:
     clearfd(epoll_fd,params,key_fd)
    elif fd_time < last_min_time:
     last_min_time = fd_time
if __name__ == "__main__":
 reload(sys)
 sys.setdefaultencoding('utf8')
 InitLog()
 port = int(sys.argv[1])
 try:
  listen_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
 except socket.error, msg:
  logger.error("create socket failed")
 try:
  listen_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 except socket.error, msg:
  logger.error("setsocketopt SO_REUSEADDR failed")
 try:
  listen_fd.bind(('', port))
 except socket.error, msg:
  logger.error("bind failed")
 try:
  listen_fd.listen(10240)
  listen_fd.setblocking(0)
 except socket.error, msg:
  logger.error(msg)
 child_num = 19
 c = 0
 while c < child_num:
  c = c + 1
  newpid = os.fork()
  if newpid == 0:
   run_main(listen_fd)
 run_main(listen_fd)

总结

以上所述是小编给大家介绍的Python代码实现http/https代理服务器,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
pycharm 使用心得(九)解决No Python interpreter selected的问题
Jun 06 Python
Python随机生成数模块random使用实例
Apr 13 Python
用Python进行行为驱动开发的入门教程
Apr 23 Python
python判断字符串编码的简单实现方法(使用chardet)
Jul 01 Python
python3.6连接MySQL和表的创建与删除实例代码
Dec 28 Python
Python3.x爬虫下载网页图片的实例讲解
May 22 Python
python3爬虫学习之数据存储txt的案例详解
Apr 24 Python
python 使用socket传输图片视频等文件的实现方式
Aug 07 Python
关于ZeroMQ 三种模式python3实现方式
Dec 23 Python
如何通过python实现IOU计算代码实例
Nov 02 Python
Python中用xlwt制作表格实例讲解
Nov 05 Python
总结python多进程multiprocessing的相关知识
Jun 29 Python
Django admin model 汉化显示文字的实现方法
Aug 12 #Python
Python简易版图书管理系统
Aug 12 #Python
django drf框架中的user验证以及JWT拓展的介绍
Aug 12 #Python
python中eval与int的区别浅析
Aug 11 #Python
将Python文件打包成.EXE可执行文件的方法
Aug 11 #Python
python多线程同步实例教程
Aug 11 #Python
pandas的to_datetime时间转换使用及学习心得
Aug 11 #Python
You might like
屏蔽PHP默认设置中的Notice警告的方法
2016/05/20 PHP
php正则去除网页中所有的html,js,css,注释的实现方法
2016/11/03 PHP
PHP编辑器PhpStrom运行缓慢问题
2017/02/21 PHP
mac pecl 安装php7.1扩展教程
2019/10/17 PHP
tp5修改(实现即点即改)
2019/10/18 PHP
newxtree.js代码
2007/03/13 Javascript
取得窗口大小 兼容所有浏览器的js代码
2011/08/09 Javascript
文本框文本自动补全效果示例分享
2014/01/19 Javascript
Juery解决tablesorter中文排序和字符范围的方法
2015/05/06 Javascript
JavaScript数组的一些奇葩行为
2016/01/25 Javascript
D3.js实现折线图的方法详解
2016/09/21 Javascript
javascript编写简易计算器
2017/05/06 Javascript
详解vue-cli项目中的proxyTable跨域问题小结
2018/02/09 Javascript
微信小程序中使用echarts的实现方法
2019/04/24 Javascript
[49:54]Ti4 循环赛第三日 LGD vs Titan
2014/07/12 DOTA
[04:54]DOTA2-DPC中国联赛1月31日Recap集锦
2021/03/11 DOTA
Python 类与元类的深度挖掘 I【经验】
2016/05/06 Python
详解python的几种标准输出重定向方式
2016/08/15 Python
Python数据可视化正态分布简单分析及实现代码
2017/12/04 Python
flask框架中勾子函数的使用详解
2018/08/01 Python
python 堆和优先队列的使用详解
2019/03/05 Python
Python实现密钥密码(加解密)实例详解
2020/04/26 Python
python判断all函数输出结果是否为true的方法
2020/12/03 Python
PurCotton全棉时代官网:100%天然棉花生产的生活护理用品
2016/11/18 全球购物
印度最好的在线药品订购网站:PharmEasy
2018/11/30 全球购物
allbeauty美国:英国在线美容店
2019/03/11 全球购物
伯克斯奥特莱斯:Burkes Outlet
2019/03/30 全球购物
Hawes & Curtis官网:英国经典品牌
2019/07/27 全球购物
教育技术学专业职业规划书
2014/03/03 职场文书
公司年会策划方案
2014/05/17 职场文书
乡镇机关党员民主评议表自我评价
2014/09/21 职场文书
法学专业毕业实习自我鉴定2014
2014/09/27 职场文书
董事长开业致辞
2015/07/29 职场文书
公司员工培训管理制度
2015/08/04 职场文书
python pygame 开发五子棋双人对弈
2022/05/02 Python
MySQL 数据表操作
2022/05/04 MySQL