1.代理流程图

2.实现代码

#! usr/bin/python2
import sys
import socket
import thread #handle local buffer
def request_handler(buffer):
return buffer
#handle remote buffer
def response_handler(buffer):
return buffer #receive data from remote_server or remote_client
def receive_from(connection):
buffer=""
connection.settimeout(2)
try:
while True:
data=connection.recv(4096)
if not data:
break
buffer+=data
except:
pass
return buffer def server_loop(local_host,local_port,remote_host,remote_port,receive_first):
#作为服务器监听并接受remote_client连接
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((local_host,local_port))
server.listen(5) while True:
client_socket,addr=server.accept()
print("other client has connected to me")
#打开一个线程处理与remote_client的连接,并作为客户端连接remote_server
thread.start_new_thread(proxy_handler, (client_socket,remote_host,remote_port,receive_first)) def proxy_handler(client_socket,remote_host,remote_port,receive_first):
#作为客户端连接remote_server
remote_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
remote_socket.connect((remote_host,remote_port))
#判断是否先接受来自remote_server的信息
if receive_first:
remote_buffer=receive_from(remote_socket)
print("from remote_server:\n"+remote_buffer)
if len(remote_buffer):
print("sending to client")
client_socket.send(remote_buffer)
while True:
#接受来自remote_client的信息并存储在local_buffer
#将local_buffer的信息再发送到remote_server
local_buffer=receive_from(client_socket)
if len(local_buffer):
print("from client:\n"+local_buffer)
local_buffer=request_handler(local_buffer)
remote_socket.send(local_buffer)
print("sent to remote_server")
#接受来自remote_server的信息并存储在remote_buffer
#将remote_buffer的信息再发送到remote_client
remote_buffer=receive_from(remote_socket)
if len(remote_buffer):
print("from remote_server:\n"+remote_buffer)
remote_buffer=request_handler(remote_buffer)
client_socket.send(remote_buffer)
print("sent to client") def main():
if len(sys.argv[1:]) != 5:
print("failed to input")
sys.exit(0) local_host=sys.argv[1]
local_port=int(sys.argv[2]) remote_host=sys.argv[3]
remote_port=int(sys.argv[4]) receive_first=sys.argv[5] if "true" in receive_first:
receive_first=True
else:
receive_first=False server_loop(local_host,local_port,remote_host,remote_port,receive_first)
main()

python实现tcp代理的更多相关文章

  1. Black Hat Python之#2:TCP代理

    在本科做毕设的时候就接触到TCP代理这东西,当时需要使用代理来对发送和收到的数据做修改,同时使用代理也让我对HTTP协议有了更深的了解. TCP Proxy用到的一个主要的东西就是socket.pro ...

  2. [转]使用 mitmproxy + python 做拦截代理

    使用 mitmproxy + python 做拦截代理   本文是一个较为完整的 mitmproxy 教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 ...

  3. 一个简单的tcp代理实现

    There are a number of reasons to have a TCP proxy in your tool belt, bothfor forwarding traffic to b ...

  4. nginx TCP 代理& windows傻瓜式安装

    一.下载nginx Windows http://nginx.org/en/download.html 二.解压到目录 三.进入目录并start nginx.exe即可启动 cd d:/java/ng ...

  5. nginx : TCP代理和负载均衡的stream模块

    一直以来,Nginx 并不支持tcp协议,所以后台的一些基于TCP的业务就只能通过其他高可用负载软件来完成了,比如Haproxy. 这算是一个nginx比较明显的缺憾.不过,在1.90发布后这个认知将 ...

  6. iOS进阶之TCP代理鉴权过程

    这段时间接触了网络代理,而自己的任务是完成TCP和UDP的网络代理,所以在这里写些自己的理解吧. 这篇文章先介绍一下TCP代理的鉴权过程(采用的是用户名和密码鉴权),下一篇文章再介绍UDP代理的鉴权过 ...

  7. 早期nginx tcp代理(基于patch实现)

    nginx tcp代理功能由nginx_tcp_proxy_module模块提供,同时监测后端主机状态.该模块包括的模块有: ngx_tcp_module, ngx_tcp_core_module, ...

  8. Nginx 配置TCP代理

    Nginx 1.9 版本以后增加了stream模块,可以对tcp,udp请求进行代理和负载均衡了,今天来体验一下首先编译安装过程configure的时候增加选项 --with-stream --wit ...

  9. python之tcp自动重连

    操作系统: CentOS 6.9_x64 python语言版本: 2.7.13 问题描述 现有一个tcp客户端程序,需定期从服务器取数据,但由于种种原因(网络不稳定等)需要自动重连. 测试服务器示例代 ...

随机推荐

  1. Mac中好用的快捷键

    1.safari safariy页面刷新:Command+R,类似于Win系统里面的F5

  2. Cisco Port-Channel 设置(链路聚合--重点)

    Port-Channel 的在实际工作中的主要作用是将两个或多个端口捆绑成为一个虚拟通道. interface Port-channel1 description port(1/0/5-6) swit ...

  3. 一百四十三:CMS系统之评论布局和功能一

    模型 class CommentModel(db.Model): """ 评论 """ __tablename__ = 'comment' ...

  4. 使用python3脚本部署mariadb主从架构

    环境准备 一个脚本自动部署master服务 另一个部署slave服务 关闭主从节点的防火墙 以及事先设置好root远程登陆的权限. master import paramikossh=paramiko ...

  5. 第三方框架----FMDB的使用

    以下是SQLite API进行封装的库FMDB的简单使用 : FMDB框架的下载地址 :https://github.com/ccgus/fmdb 代码如下 : ////  ViewControlle ...

  6. win7下安装IIS7

    在Windows 7下如何安装IIS7,以及IIS7在安装过程中的一些需要注意的设置,以及在IIS7下配置ASP的正确方法. 在Windows 7下面IIS7的安装方法: 一.进入Windows 7的 ...

  7. async/await小知识点

    一.使用async修饰的函数,返回的内容就是一个Promise,因此可以使用Promise对象所有的方法,如then等 二.在async函数中使用await是否会影响其他代码的执行,答案是:不会.

  8. 使用jquery操作iframe中的元素

    使用jquery操作iframe中的元素<iframe src="/test/demo.htm" width="99%" height="300 ...

  9. js 实现数字格式化(货币格式)几种方法

    // 方法一 function toThousands(num) { var result = [ ], counter = 0; num = (num || 0).toString().split( ...

  10. C++中静态成员函数和普通成员函数存储方式相同

    先从一个示例查看类的创建过程中,静态成员函数和普通成员函数的存储区别. #include "stdafx.h" #include<iostream> #include& ...