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. java生成二维码打印到浏览器

    java生成二维码打印到浏览器   解决方法: pom.xml的依赖两个jar包: <!-- https://mvnrepository.com/artifact/com.google.zxin ...

  2. 在Mac 搭建robotframework 环境 遇到ride.py 打不开的方法(没试过,先记录在此)

    折腾来一下午,遇到了好多坑 坑 1.不要用pip 下载wxpython 2.不要用mac自带的python 3.不要自己下载wxpython 步骤: 1. 安装homebrew, /usr/bin/r ...

  3. Oracle SQL 脚本跟踪

    NC Oracle SQL 脚本跟踪 脚本: select * from v$sqlarea a and a.LAST_ACTIVE_TIME >= to_date( '2013-02-21 1 ...

  4. JS时间转换,url编码,jquery返回类型等问题

    1.当时间被转换为json格式后会被转换成 /Date(...)/ 这种格式,其中...为时间转换成妙后的一串整数 function changeDateFormat(cellval) { )); v ...

  5. maven执行过程中抛出的各类异常信息

    价值 各类异常信息分类 举例 maven源代码的模块maven-core里的各类*Exception命名的class包含里,maven执行过程中打印的各类异常日志内容 比如如下错误 错误信息分别来自( ...

  6. Nginx负载均衡中4层代理和7层代理对比

    1.4层代理和7层代理什么意思? 这里的层是OSI 7层网络模型,OSI 模型是从上往下的,越底层越接近硬件,越往上越接近软件,这七层模型分别是物理层.数据链路层.网络层.传输层.会话层.表示层.应用 ...

  7. CentOS 7 vi常用命令

    用vi打开一个yum文件 vi /usr/bin/yum 按 i 键后  进入insert模式,进入insert模式后才能进行修改 修改完成后 按esc键进入command模式, 然后:wq 保存文件 ...

  8. (CVE-2017-7269 ) IIS6.0实现远程控制

    简介 IIS 6.0默认不开启WebDAV,一旦开启了WebDAV,安装了IIS6.0的服务器将可能受到该漏洞的威胁 利用条件 Windows 2003 R2开启WebDAV服务的IIS6.0 环境搭 ...

  9. 【计算机视觉】BING: Binarized Normed Gradients for Objectness Estimation at 300fps

    BING: Binarized Normed Gradients for Objectness Estimation at 300fps Ming-Ming Cheng, Ziming Zhang, ...

  10. jqGrid全部选中

    var jqGrid = $("#jqGrid"); // 拿到所有行id var jqGridIDs = jqGrid.getDataIDs(); // 拿到所有选中行id va ...