Python 50行代码实现代理服务器(加强版)

来源(博客频道 - CSDN.NET)

From: http://blog.csdn.net/junfeng_feng/article/details/42966363

Python 50行代码实现代理服务器(加强版)

2015-01-21 13:36 2387人阅读

分类: Python(3) Linux(4)

版权声明:本文为博主原创文章,未经博主允许不得转载。

网上很多Python 50行代码实现代理服务器,但是易出现异常,导致退出。

这里的代理服务器,是指二级代理服务器。比如:A可以访问B,B可以访问C,A不能直接访问C。这时,如果在B开一个二级代理,就可实现A访问C。现有的工具有CCProxy。

这里就是使用python简单的实现一个二级代理。

#coding=utf-8
import socket
import select
import sys
import threading
import time
import logging
import os
logsDir = "logs"
if not os.path.isdir(logsDir):
	os.mkdir(logsDir)

logging.basicConfig(level=logging.DEBUG,
				format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
				datefmt='%a, %d %b %Y %H:%M:%S',
				filename='logs/logs.log',
				filemode='a')

#C的IP和端口
to_addr = ('127.0.0.1', 8087)

maxConnetions = 32
class Proxy:
	def __init__(self, addr):
		self.proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.proxy.bind(addr)
		self.proxy.listen(maxConnetions)
		self.inputs = {self.proxy:None}
		self.route = {}

	def serve_forever(self):
		logging.info('proxy listen...')
		while 1:
			readable, _, _ = select.select(list(self.inputs.keys()), [], [])
			for self.sock in readable:
				if self.sock == self.proxy:
					self.on_join()
				else:
					try:
						data = self.sock.recv(8192)
					except Exception, e:
						logging.error(str(e))
						self.on_quit()
						continue
					
					if not data:
						self.on_quit()
					else:
						try:
							self.route[self.sock].send(data)
						except Exception, e:
							logging.error(str(e))
							self.on_quit()
							continue

	def on_join(self):
		client, addr = self.proxy.accept()
		logging.info("proxy client " + str(addr) + 'connect')
		forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		try:
			forward.connect(to_addr)
		except Exception, e:
			logging.error(str(e))
			client.close()
			return
		self.inputs [client] = None
		self.inputs [forward] = None


		self.route[client] = forward
		self.route[forward] = client

	#删除连接
	def on_quit(self):
		ls = [self.sock]
		if self.sock in self.route:
			ls.append(self.route[self.sock])
		for s in ls:
			if s in self.inputs:
				del self.inputs[s]
			if s in self.route:
				del self.route[s]
			s.close()
			
if __name__ == "__main__":
	try:
		Proxy(('', 8192)).serve_forever()
	except KeyboardInterrupt, e:
		logging.error("KeyboardInterrupt" + str(e))

Link: http://www.asm32.net/article_details.aspx?id=7250


浏览次数 0 发布时间 2017/6/30 1:25:10 从属分类 Python 【评论】【 】【打印】【关闭
 
| www.asm32.net | 2006版 | 资料中心 | linux | asm/asm32 | C/C++ | VC++ | java | Python | 书签 | ASP.Net书签 | 京ICP备09029108号-1