0

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!

4

1 回答 1

0

我没有彻底分析您的代码,但在我看来,您的 linux 机器很可能在端口 8080(使用的标准端口)上运行某种代理服务。

尝试netstat -tunlp命令,看看是否有东西没有占用那个端口。

编辑:另一个场合的专业提示:在 Windows Skype 上默认使用端口 80 和 433,它可以在属性中切换。

于 2015-07-25T15:12:51.583 回答