Advertisement
cessnaflyer

repetier-proxy.py

May 2nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import http.server
  4. import socketserver
  5. from urllib import request,parse,error
  6. PORT=3345
  7. class Proxy(http.server.BaseHTTPRequestHandler):
  8.  def do_POST(self):
  9.   newpath = "http://localhost:3344" + self.path
  10.   req = request.Request(newpath)
  11.   for h in self.headers.items():
  12.    req.add_header(h[0],h[1])
  13.   inputdata = ''
  14.   contlen = self.headers.get('Content-length')
  15.   enc = self.headers.get('Transfer-Encoding')
  16.   if contlen:
  17.    inputdata = self.rfile.read(int(contlen))
  18.   elif enc == 'chunked':
  19.    line = self.rfile.readline()
  20.    inputdata = line
  21.    readsize = int(line,16)
  22.    while(readsize > 0):
  23.     inputdata = inputdata + self.rfile.read(readsize)
  24.     inputdata = inputdata + self.rfile.readline()
  25.     line = self.rfile.readline()
  26.     inputdata = inputdata + line
  27.     readsize = int(line,16)
  28.   else:
  29.    inputdata = ''
  30.   req.data = inputdata
  31.   try:
  32.    result = request.urlopen(req)
  33.   except error.HTTPError as err:
  34.    self.send_error(err.code,err.reason)
  35.    return
  36.   self.send_response(result.status)
  37.   for h in result.getheaders():
  38.    self.send_header(h[0],h[1])
  39.   self.end_headers()
  40.   resdata = result.read()
  41.   self.wfile.write(resdata)
  42.  
  43. #  pdb.set_trace()
  44.  
  45. httpd = socketserver.ForkingTCPServer(('',PORT),Proxy)
  46. try:
  47.  httpd.serve_forever()
  48. except (Exception,KeyboardInterrupt):
  49.  httpd.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement