一个代理转发的程序
今天写完了一个代理转发的程序
本来是为了qq写的,但是qq使用connect连接,导致连接失败
其他的软件业可以使用这个程序了
#coding: utf-8
import sys
import time
import socket
import select
import struct
import threading
import thread
import SocketServer
import logging
import binascii
class MyHandler(SocketServer.BaseRequestHandler):
def handle(self):
datasum=''
try:
logging.debug(str(id(self.request))+'wangyb1')
while 1:
dataReceived = self.request.recv(1024)
datasum=datasum+dataReceived
#if not dataReceived: break
#self.request.send(dataReceived)
try:
pos=datasum.index('\n\r\n')
logging.debug(str(id(self.request))+':ok->'+str(pos)+'['+binascii.b2a_hex(datasum)+']')
#logging.debug(str(id(self.request))+':ok->'+str(pos)+'['+datasum+']')
break
except ValueError:
logging.debug(str(id(self.request))+':['+datasum+']')
i=0
#HOST='2001:638:902:1::11'
HOST='proxy.ipv6.uni-leipzig.de'
PORT=3128
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.connect((HOST, PORT))
logging.debug(str(id(self.request))+'conn ok'+str(s))
s.send(datasum)
datarecv = s.recv(1024000)
s.close()
logging.debug(str(id(self.request))+str(len(datarecv))+' datarecv=['+datarecv+']')
self.request.send(datarecv)
except Exception:
logging.error(str(id(self.request))+'['+str(Exception)+']')
if __name__=='__main__':
print 'this这是hh'
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',filename='logsl.txt',filemode='a+')
myServer = SocketServer.ThreadingTCPServer(('',808), MyHandler)
logging.info(str(myServer))
myServer.serve_forever()