googlecode :https://code.google.com/archive/p/python-proxy/source/default/source

# -*- coding: cp1252 -*-
# <PythonProxy.py>
#
#Copyright (c) <2009> <F醔io Domingues - fnds3000 in gmail.com>
#
#Permission is hereby granted, free of charge, to any person
#obtaining a copy of this software and associated documentation
#files (the "Software"), to deal in the Software without
#restriction, including without limitation the rights to use,
#copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the
#Software is furnished to do so, subject to the following
#conditions:
#
#The above copyright notice and this permission notice shall be
#included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#OTHER DEALINGS IN THE SOFTWARE. """\
Copyright (c) <2009> <F醔io Domingues - fnds3000 in gmail.com> <MIT Licence> **************************************
*** Python Proxy - A Fast HTTP proxy ***
************************************** Neste momento este proxy ?um Elie Proxy. Suporta os m閠odos HTTP:
- OPTIONS;
- GET;
- HEAD;
- POST;
- PUT;
- DELETE;
- TRACE;
- CONENCT. Suporta:
- Conex鮡s dos cliente em IPv4 ou IPv6;
- Conex鮡s ao alvo em IPv4 e IPv6;
- Conex鮡s todo o tipo de transmiss鉶 de dados TCP (CONNECT tunneling),
p.e. liga珲es SSL, como ?o caso do HTTPS. A fazer:
- Verificar se o input vindo do cliente est?correcto;
- Enviar os devidos HTTP erros se n鉶, ou simplesmente quebrar a liga玢o;
- Criar um gestor de erros;
- Criar ficheiro log de erros;
- Colocar excep珲es nos s韙ios onde ?previs韛el a ocorr阯cia de erros,
p.e.sockets e ficheiros;
- Rever tudo e melhorar a estrutura do programar e colocar nomes adequados nas
vari醰eis e m閠odos;
- Comentar o programa decentemente;
- Doc Strings. Funcionalidades futuras:
- Adiconar a funcionalidade de proxy an髇imo e transparente;
- Suportar FTP?. (!) Aten玢o o que se segue s?tem efeito em conex鮡s n鉶 CONNECT, para estas o
proxy ?sempre Elite. Qual a diferen鏰 entre um proxy Elite, An髇imo e Transparente?
- Um proxy elite ?totalmente an髇imo, o servidor que o recebe n鉶 consegue ter
conhecimento da exist阯cia do proxy e n鉶 recebe o endere鏾 IP do cliente;
- Quando ?usado um proxy an髇imo o servidor sabe que o cliente est?a usar um
proxy mas n鉶 sabe o endere鏾 IP do cliente;
?enviado o cabe鏰lho HTTP "Proxy-agent".
- Um proxy transparente fornece ao servidor o IP do cliente e um informa玢o que
se est?a usar um proxy.
S鉶 enviados os cabe鏰lhos HTTP "Proxy-agent" e "HTTP_X_FORWARDED_FOR". """ import socket, thread, select __version__ = '0.1.0 Draft 1'
BUFLEN = 8192
VERSION = 'Python Proxy/'+__version__
HTTPVER = 'HTTP/1.1' class ConnectionHandler:
def __init__(self, connection, address, timeout):
self.client = connection
self.client_buffer = ''
self.timeout = timeout
self.method, self.path, self.protocol = self.get_base_header()
if self.method=='CONNECT':
self.method_CONNECT()
elif self.method in ('OPTIONS', 'GET', 'HEAD', 'POST', 'PUT',
'DELETE', 'TRACE'):
self.method_others()
self.client.close()
self.target.close() def get_base_header(self):
while 1:
self.client_buffer += self.client.recv(BUFLEN)
end = self.client_buffer.find('\n')
if end!=-1:
break
print '%s'%self.client_buffer[:end]#debug
data = (self.client_buffer[:end+1]).split()
self.client_buffer = self.client_buffer[end+1:]
return data def method_CONNECT(self):
self._connect_target(self.path)
self.client.send(HTTPVER+' 200 Connection established\n'+
'Proxy-agent: %s\n\n'%VERSION)
self.client_buffer = ''
self._read_write() def method_others(self):
self.path = self.path[7:]
i = self.path.find('/')
host = self.path[:i]
path = self.path[i:]
self._connect_target(host)
self.target.send('%s %s %s\n'%(self.method, path, self.protocol)+
self.client_buffer)
self.client_buffer = ''
self._read_write() def _connect_target(self, host):
i = host.find(':')
if i!=-1:
port = int(host[i+1:])
host = host[:i]
else:
port = 80
(soc_family, _, _, _, address) = socket.getaddrinfo(host, port)[0]
self.target = socket.socket(soc_family)
self.target.connect(address) def _read_write(self):
time_out_max = self.timeout/3
socs = [self.client, self.target]
count = 0
while 1:
count += 1
(recv, _, error) = select.select(socs, [], socs, 3)
if error:
break
if recv:
for in_ in recv:
data = in_.recv(BUFLEN)
if in_ is self.client:
out = self.target
else:
out = self.client
if data:
out.send(data)
count = 0
if count == time_out_max:
break def start_server(host='', port=8080, IPv6=False, timeout=60,
handler=ConnectionHandler):
if IPv6==True:
soc_type=socket.AF_INET6
else:
soc_type=socket.AF_INET
soc = socket.socket(soc_type)
soc.bind((host, port))
print "Serving on %s:%d."%(host, port)#debug
soc.listen(0)
while 1:
thread.start_new_thread(handler, soc.accept()+(timeout,)) if __name__ == '__main__':
start_server()

最新文章

  1. js入门学习~ 运动应用小例
  2. TortoiseSVN status cache占用CPU高
  3. javascript中数组常用的方法
  4. 2014江西理工大学C语言程序竞赛高级组
  5. 转:strcmp函数实现及分析
  6. CSS Devices可以让你在线直接获取使用CSS写的Mobile外形。
  7. Count Color POJ--2777
  8. linux 的 ping 原理
  9. [转]SHSH, APTicket以及iOS降級
  10. wps 操作
  11. DataPipeline | 享物说产品负责人夏凯:数据驱动的用户增长实战
  12. docker基础学习(一)
  13. Java并发编程-阻塞队列(BlockingQueue)的实现原理
  14. 谈谈关于PHP连接数据库的两种方法(PDO&amp;Mysqli)
  15. python---ORM之SQLAlchemy(2)外键使用
  16. [HDU5685]Problem A
  17. 运行Maven工程中修改tomcat端口
  18. 基于folly的AtomicIntrusiveLinkedList无锁队列进行简单封装的多生产多消费模型
  19. sort命令的k选项大讨论【转】
  20. pycharm的安装和激活

热门文章

  1. Python之路----------shutil模块
  2. TT3
  3. Leetcode: Sentence Screen Fitting
  4. spring ioc三种注入方式
  5. 镁光c400-MTFDDAK064M固态硬盘更新固件
  6. spring cloud 学习研究- spring-cloud-microservice-example
  7. VB.NET中Form窗体运行时,按ESC退出全屏状态
  8. 获取浏览器信息 c#
  9. WebBrowser内核指定
  10. MiniUI中DataGrid数据的载入