Necesito un servidor el cual pueda responder a varias peticiones post que se le pidan y retorne su respectiva respuesta.
Mi código representa un simple server pero solo hace un petición a la vez. Soy nuevo utilizando multiprocessing, no sé como implementarlo correctamente.
# -*- 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()