Author Topic: [PYTHON] Proxy Server 19 Lines of Code!  (Read 3409 times)

0 Members and 1 Guest are viewing this topic.

Offline Fl0urite

  • /dev/null
  • *
  • Posts: 15
  • Cookies: -16
    • View Profile
[PYTHON] Proxy Server 19 Lines of Code!
« on: February 28, 2014, 01:47:29 am »
19 lines of code for a proxy server (that works amazingly too!)
It works for HTTP, and HTTPS!
Maybe it works for a few other things?
made by yours truly!
Code: [Select]

import socket
import urllib2
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('',8080))
s.listen(1000)
def HandleProxy(c):
   Request=c.recv(1024)
   Parse=Request.split()
   try:
      req = urllib2.Request(Parse[1])
      html=urllib2.urlopen(req).read()
   except (urllib2.URLError,urllib2.HTTPError) as e:
      html="<b>Proxy error: "+e.reason[1]+"</b>"
   c.send(html)
   print "Requested data:\n"+ Request
while 1:
      (c,addr)=s.accept()
      print addr[0]+":"+str(addr[1])+" connected!"
      HandleProxy(c)


For those of you who want a multi-threaded version here you go (not 19 lines lol):
Code: [Select]

#Simple python base proxy server
#coded by fl0urite!
from threading import Thread
import socket
import urllib2
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('',8080))
s.listen(1000)
def HandleProxy(c):
   Request=c.recv(1024)
   Parse=Request.split()
   try:
      req = urllib2.Request(Parse[1])
      html=urllib2.urlopen(req).read()
   except (urllib2.URLError,urllib2.HTTPError) as e:
      html="<b>Proxy error: "+e.reason[1]+"</b>"
   c.send(html)
   print "Requested data:\n"+ Request
while 1:
   (c,addr)=s.accept()
   print addr[0]+":"+str(addr[1])+" connected!"
   thread = Thread(target=HandleProxy(c))
   thread.start()


Have fun!

Staff note: edit button. Do you use it?
Fl0urite's reply: On this forum it is the modify button, so no.
« Last Edit: March 01, 2014, 12:40:21 am by Fl0urite »
If you feel my post was interesting or stood out, give me a cookie!