ich wollte mir ne kleine HTTP Proxy schreiben. Es funktioniert auch, nur etwas sehr langsam.
Eine Idee an was das liegen könnte?
Hab das Beispiel von: http://www.geocities.com/ubershmekel/microproxy.txt
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re, time, sys
import socket
import threading
regex = re.compile(r'http://(.*?)/', re.IGNORECASE)
def log(msg, channel):
if channel != 2:
datum = time.strftime("[%d-%m-%Y %H:%M:%S]", time.gmtime())
sys.stdout.write("%s %s\n" % (datum, msg))
else:
datum = time.strftime("[%d-%m-%Y %H:%M:%S]", time.gmtime())
sys.stderr.write("%s %s\n" % (datum, msg))
class ConnectionThread(threading.Thread):
def __init__(self, (conn,addr)):
self.conn = conn
self.addr = addr
threading.Thread.__init__(self)
def run(self):
data = self.conn.recv(1024*1024)
host = regex.search(data).groups()[0]
log(host, 1)
if host == "google.at":
temp = """HTTP/1.1 302 Found
Location: http://www.wikipedia.org/index.php
"""
self.conn.send(temp)
else:
request = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#request.settimeout(6)
request.connect((host,80))
request.send(data)
reply = ''
while 1:
temp = request.recv(1024)
if ('' == temp):
break
self.conn.send(temp)
self.conn.close()
class ProxyThread(threading.Thread):
def __init__(self, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind(('localhost', port))
threading.Thread.__init__(self)
def run(self):
self.sock.listen(10)
while 1:
temp = ConnectionThread(self.sock.accept())
temp.daemon = True
temp.start()
if __name__ == "__main__":
proxy = ProxyThread(8000)
proxy.daemon = True
proxy.start()
print "Started a proxy on port 8000"