I'm trying to write a program that changes the content of a webpage with some user given parameters. After several tries and some googling, i've managed to get some results (I put the webserver in a thread in order to continue other tasks in the main routine). It seems to run pretty smooth in windows, but I always got a
socket.error: [Errno 98] Address already in use
when I try it from my linux machine (the whole thing must run on raspian at the end).
I read something here that explains that the SO_REUSEADDR socket option (I think it concerns with) have different behaviours among different OS (and, if I haven't misunderstood, it "works" on Windows thanks to a bug), but I can't find a solution to get it work this way on Linux for this option is already set to True by default.
Here's a simplified absract of the code that generates the error:
import web
probe = raw_input("1st content: ")
web.web(probe)
probe = raw_input("2nd content: ")
web.web(probe)
raw_input()
with this module (web.py)
# -*- coding: utf8 -*-
def web(text):
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()
self.wfile.write(bytes(text.encode('utf-8')))
self.wfile.close()
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
allow_reuse_address = True
server = ThreadedHTTPServer(('', 8080), Handler)
print 'Now serving, use <Ctrl+C> to break'
thread = threading.Thread(target = server.serve_forever)
thread.daemon = True
thread.start()
Is there a real solution to get rid of this error? Or is there a better way to manage the handler in orger to acchieve my goal?
EDIT: the error occurs after the input of the "2nd content". The 1st instance runs flawlessly, so I can tell for sure that the socket is not used by anything else.
Thanks to all!