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. 005-多线程-集合-Map-ConcurrentSkipListMap

    一.概述 ConcurrentSkipListMap是线程安全的有序的哈希表,适用于高并发的场景. ConcurrentSkipListMap和TreeMap,它们虽然都是有序的哈希表.但是,第一,它 ...

  2. osg qt ifc

    ui_ifcproject_20190702.h #pragma once /************************************************************* ...

  3. js下利用userData实现客户端保存表单数据

    对于多数网页制作的朋友,实现在客户端保存在网页表单上的信息,比较多的是采用Cookie技术来实现,这些功能例如:下拉列表框选择的选项,文本框输入的数据等. 事实上,我们可以利用微软DHTML默认行为中 ...

  4. Cas(01)——简介

    Cas的全称是Centeral Authentication Service,是对单点登录SSO(Single Sign On)的一种实现.其由Cas Server和Cas Client两部分组成,C ...

  5. iOS-UITabbar自定义

    [self createCustomTabBar]; -(void)createCustomTabBar{    //创建一个UIImageView,作为底图    UIImageView *bgVi ...

  6. spring mvc 后端获得前端传递过来的参数的方法

    1.通过HttpServletRequest 获得 HttpServletRequest.getParameter(参数名),可以获得form表单中传递的参数,或ajax或url中传递过来的参数,如果 ...

  7. 【馨儿收藏】群星《2019最新好听DJ舞曲精选》全系列【WAV/在线/百度】(持续更新)

    本人作为一名音乐发烧友,一直喜欢追求无损音乐,平时在开发编程无聊的时候,希望享受音乐的过程,追求完美,我这边整理了一系列的比较不错,新的好听的无损音乐,希望大家能够喜欢. [馨儿收藏]群星<20 ...

  8. AutoMapper扩展帮助类

    /// <summary> /// AutoMapper扩展帮助类 /// </summary> public static class AutoMapperExtension ...

  9. Java线程池的使用方式,核心运行原理、以及注意事项

    为什么需要线程池 java中为了提高并发度,可以使用多线程共同执行,但是如果有大量线程短时间之内被创建和销毁,会占用大量的系统时间,影响系统效率. 为了解决上面的问题,java中引入了线程池,可以使创 ...

  10. CNN-3: VGGNet 卷积神经网络模型

    1.VGGNet 模型简介 VGG Net由牛津大学的视觉几何组(Visual Geometry Group)和 Google DeepMind公司的研究员一起研发的的深度卷积神经网络,在 ILSVR ...