How to make a multiprocess server? -Server-multiprocessing-

0

I need a server that can respond to several post requests that are requested and return your respective response.

My code represents a simple server but only makes one request at a time. I am new using multiprocessing, I do not know how to implement it correctly.

# -*- coding: utf-8 -*-
# Autor: Diego Lopez

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import multiprocessing
from selenium.common.exceptions import TimeoutException
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer
import json
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import signal

class S(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        self.wfile.write("<html><body><h1>hi!</h1></body></html>")

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        # Doesn't do anything with posted data
        content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
        post_data = self.rfile.read(content_length) # <--- Gets the data itself
        self._set_headers()
        try:
            data_string = json.loads(post_data)
        except:
            self.wfile.write('{"error" : "JSON"}')
            return

        placa = str(data_string["placa"])
        placa = placa.encode("utf8")
        print post_data
        scraping(placa,self)


def run(server_class=HTTPServer, handler_class=S, port=6464):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'Starting httpd...'
    server_process = multiprocessing.Process(httpd.serve_forever())
    server_process.daemon = True
    server_process.start()



def scraping(placa,self):
    self.wfile.write(placa)

if __name__ == "__main__":
    from sys import argv

    if len(argv) == 2:
        run(port=int(argv[1]))
    else:
        run()
    
asked by Diego Lopez 18.01.2018 в 21:55
source

1 answer

0

The answer to the question was a little complicated but I found a functional code that served me for what I needed.

Use ThreadedHTTPServer and implement it like this.

The code is as follows:

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
class Handler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        message =  threading.currentThread().getName()
        self.wfile.write(message)
        self.wfile.write('\n')
        return

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""

if __name__ == '__main__':
    server = ThreadedHTTPServer(('localhost', 8080), Handler)
    print 'Starting server, use <Ctrl-C> to stop'
    server.serve_forever()
    
answered by 19.01.2018 в 15:54