On how to implement reverse proxy 1024 in Python

Now the implementation of the reverse proxy is mainly using Nginx, google version of the implementation of Python, most of them are implemented by Flask+requests. Too young. What should I do if I encounter a post request?

The following example shows how to implement reverse proxy with no brain tcp forwarding. The instance website is 1024, based on Python 3.

First, open a tcp server server and listen on port 1024:

import socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 1024))
s.listen(1500)

Then, accept a connection from a browser, no brain to receive the http request packet:

while 1:
  conn ,addr = s.accept()
  headers = ''
  while 1:
    buf = conn.recv(2048).decode('utf-8')
    headers += buf
    if len(buf) < 2048:
      break

Modify the headers, change the target host (127.0.0.1:1024) to 1024 domain name, close the long connection and compression, and modify the web page returned by the server:

headers = headers.replace('127.0.0.1:1024', 't66y.com')\
                 .replace('keep-alive', 'close')\
                 .replace('gzip','')

Open a new tcp, connect 1024, send http request:

s1 = socket.socket()
s1.connect(('t66y.com', 80))
s1.sendall(headers.encode())

Receive webpage:

while 1:
  try:
    buf = s1.recv(1024*8)
  except socket.timeout as e:
    print(e)
    break
  resp += buf
  if not buf or\
    buf.startswith(b'WebSocket') and buf.endswith(b'\r\n\r\n'):
    break

Modify the content of the webpage and replace the host as the local machine:

resp = resp.replace(b'Content-Encoding: gzip\r\n', b'')\
           .replace(b'Transfer-Encoding: chunked\r\n', b'')\
           .replace(b't66y.com', b'bjgong.tk:1024')

Finally, return the page to the browser:

conn.sendall(resp)
conn.close()

After the code runs, the browser openshttp://127.0.0.1:1024/index.php The one that is presented is the 1024 homepage, excited!
The code is as follows:

import socket

def main():
    s = socket.socket()
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(('0.0.0.0', 1024))
    s.listen(1500)
    while 1:
        conn, addr = s.accept()
        print(addr)
        headers = ''
        while 1:
            buf = conn.recv(2048).decode('utf-8')
            headers += buf
            if len(buf) < 2048:
                break

        headers = headers.replace('127.0.0.1:1024', 't66y.com')\
                         .replace('keep-alive', 'close')\
                         .replace('gzip','')
        print(headers)
        s1 = socket.socket()
        s1.connect(('t66y.com', 80))
        s1.sendall(headers.encode())

        resp = b''
        while 1:
            try:
                buf = s1.recv(1024*8)
            except socket.timeout as e:
                print(e)
                break
                
            resp += buf
            if not buf or\
               buf.startswith(b'WebSocket') and buf.endswith(b'\r\n\r\n'):
                break

        resp = resp.replace(b'Content-Encoding: gzip\r\n', b'')\
                   .replace(b't66y.com', b'bjgong.tk:1024')
        print('send to', addr)
        conn.sendall(resp)
        conn.close()


main()

This simple reverse proxy does not support https website, its performance is not good, it needs to be optimized. Please change the domain name of 1024 (t66y.com) to other websites when you demonstrate at home, otherwise it will be 2333.
If you want to hang on vps, change 127.0.0.1 to your domain name or ip (no domain name).

EOF

Intelligent Recommendation

Use Nginx to implement reverse proxy

1. Proxy server 1. What is a proxy server proxy server. When the client sends a request, it will not directly send it to the destination host, but first send it to the proxy server. After the proxy se...

Understanding of Nginx to implement reverse proxy

1 How users access the server 1.1 The user accesses the local database There are two deployment locations for the database, local and remote. The route to go when accessing the local service database ...

Nginx reverse proxy to implement WSS

3. In the reverse proxy configuration file Increased position Configuration file...

Implement nginx single reverse proxy

Target server: 192.168.3.56 worker_processes 1; events {         worker_connections 1024; } http {         include mime....

http-proxy-middleware and express implement reverse proxy

$ npm install --save-dev http-proxy-middleware     https://www.cnblogs.com/resultwp/p/9945606.htm vue unconventional achieve cross-domain settings and rely proxyTable     a, is int...

More Recommendation

When and how to reverse proxy

Most of us are familiar with several agents: Forward proxy Reverse proxy "go ahead" with "Reverse" Visible in the context of the client (user agent) accessing the resource. Resourc...

Reverse proxy python twisted

Realize reverse proxy, imitate nginx Related code:http://twistedmatrix.com/documents/current/web/examples/...

How to implement dynamic proxy?

How to implement dynamic proxy? In the Java field, there are two commonly used dynamic proxy implementation methods, one is to use the JDK reflection mechanism to generate the proxy, and the other is ...

How does Vue 2.x use proxyTable to implement cross-domain requests (reverse proxy)

When the project is running, it is indispensable to set up a reverse proxy? Detailed documentationhttp-proxy-middleware  Download the official scaffolding to open the file build/dev-server.js Sea...

[Network Programming] Nginx installation and configuration under Ubuntu, and examples of how to implement reverse proxy

1. Nginx source installation enterNginx official websiteDownload the required version, this article starts withnginx-1.12.2The version is an example, downloaded to the 1.12.2 tar package. If the corre...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top