python实现tcp代理
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代理的更多相关文章
- Black Hat Python之#2:TCP代理
在本科做毕设的时候就接触到TCP代理这东西,当时需要使用代理来对发送和收到的数据做修改,同时使用代理也让我对HTTP协议有了更深的了解. TCP Proxy用到的一个主要的东西就是socket.pro ...
- [转]使用 mitmproxy + python 做拦截代理
使用 mitmproxy + python 做拦截代理 本文是一个较为完整的 mitmproxy 教程,侧重于介绍如何开发拦截脚本,帮助读者能够快速得到一个自定义的代理工具. 本文假设读者有基本的 ...
- 一个简单的tcp代理实现
There are a number of reasons to have a TCP proxy in your tool belt, bothfor forwarding traffic to b ...
- nginx TCP 代理& windows傻瓜式安装
一.下载nginx Windows http://nginx.org/en/download.html 二.解压到目录 三.进入目录并start nginx.exe即可启动 cd d:/java/ng ...
- nginx : TCP代理和负载均衡的stream模块
一直以来,Nginx 并不支持tcp协议,所以后台的一些基于TCP的业务就只能通过其他高可用负载软件来完成了,比如Haproxy. 这算是一个nginx比较明显的缺憾.不过,在1.90发布后这个认知将 ...
- iOS进阶之TCP代理鉴权过程
这段时间接触了网络代理,而自己的任务是完成TCP和UDP的网络代理,所以在这里写些自己的理解吧. 这篇文章先介绍一下TCP代理的鉴权过程(采用的是用户名和密码鉴权),下一篇文章再介绍UDP代理的鉴权过 ...
- 早期nginx tcp代理(基于patch实现)
nginx tcp代理功能由nginx_tcp_proxy_module模块提供,同时监测后端主机状态.该模块包括的模块有: ngx_tcp_module, ngx_tcp_core_module, ...
- Nginx 配置TCP代理
Nginx 1.9 版本以后增加了stream模块,可以对tcp,udp请求进行代理和负载均衡了,今天来体验一下首先编译安装过程configure的时候增加选项 --with-stream --wit ...
- python之tcp自动重连
操作系统: CentOS 6.9_x64 python语言版本: 2.7.13 问题描述 现有一个tcp客户端程序,需定期从服务器取数据,但由于种种原因(网络不稳定等)需要自动重连. 测试服务器示例代 ...
随机推荐
- Row_Number() and Rank() in SQL
1. 数据表实例数据 2. 使用Row_Number()方法给每一行数据添加一个唯一编号, 可以按照某一列进行排序. 3. 使用Partition by Column在一个Partition内进行编号 ...
- c# 遍历局域网计算机(电脑)获取IP和计算机名称
c#可以遍历局域网计算机,获取全部计算机的名称和IP地址,网上提供了相关的几种方法,并对效率进行了比较,但是没有对各种方法进行比较,以确定可以使用的情况.这篇文章将对这几种方法进行分析,以帮助了解各种 ...
- 发布一个史上最简单代码最少Javascript Timer,解决一切定时执行的问题
这个函数实现如下 function TimerStart(func,delay) { var TimerID = window.setInterval( function() { if(!func() ...
- Cognos Framework操作记录
备注:这是我单位内部的Cognos Framework配置记录,里面涉及的名字等信息在其他使用环境需要进行相应修改. Cognos数据包配置 打开CYFTest项目, 右键点击andwdb的物理视图 ...
- 下载Hadoop环境的虚拟机文件
使用的是hortonworks的hadoop环境, 下载地址:https://hortonworks.com/downloads/#
- 【Leetcode_easy】720. Longest Word in Dictionary
problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...
- CentOS安装文件共享samba
参考:https://jingyan.baidu.com/article/03b2f78cdf811c5ea237aebc.html https://www.linuxidc.com/Linux/20 ...
- Flutter FutureBuilder异步请求列表示例
Flutter的FutureBuilder列表示例 import 'package:flutter/material.dart'; import '../service/service_method. ...
- 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
[游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态 阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...
- 论文阅读 | Transformer-XL: Attentive Language Models beyond a Fixed-Length Context
0 简述 Transformer最大的问题:在语言建模时的设置受到固定长度上下文的限制. 本文提出的Transformer-XL,使学习不再仅仅依赖于定长,且不破坏时间的相关性. Transforme ...