简体   繁体   English

python瓶可以在WINDOWS上的相同地址和端口上运行两个程序

[英]python bottle can run two programs on the same address and port on WINDOWS

I just encountered a weird issue about bottle on windows. 我刚遇到有关Windows上的瓶子的奇怪问题。 When I tested the my bottle codes, I found that it could run multiple same programs on WINDOWS using same address and port. 测试瓶代码时,我发现它可以使用相同的地址和端口在WINDOWS上运行多个相同的程序。 But when you try to start multiple same program on the Linux or Mac using same address and port, it will report the below error: 但是,当您尝试使用相同的地址和端口在Linux或Mac上启动多个相同的程序时,它将报告以下错误:

socket.error: [Errno 48] Address already in use 

my bottle codes are: 我的瓶子代码是:

from bottle import route, run, template

@route('/hello/:name')
def index(name='World'):
    return template('<b>Hello {{name}} </b>', name=name)

run(host='localhost', port=9999)

Then I traced the code, from bottle to wsgiref, and finnaly found that the problem might be in the Python27\\Lib\\BaseHTTPServer.py. 然后,我跟踪了从瓶子到wsgiref的代码,最后发现问题可能出在Python27 \\ Lib \\ BaseHTTPServer.py中。 I mean when I use the the below simple codes: 我的意思是,当我使用以下简单代码时:

import BaseHTTPServer

def run(server_class=BaseHTTPServer.HTTPServer,
        handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
    server_address = ('localhost', 9999)
    print "start server on localhost 9999"
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

run()

The same issue would happen on windows. 在Windows上也会发生同样的问题。

But if I directly used the socketserver, like the below codes: 但是,如果我直接使用套接字服务器,如以下代码所示:

import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print "{} wrote:".format(self.client_address[0])
        print self.data
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
    print "Start a server on localhost:9999"
    # Create the server, binding to localhost on port 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

The same issue will not happen, I mean even on window the above socketserver codes will report the error, when you try to start another programe. 不会发生相同的问题,我的意思是,即使在窗口上,当您尝试启动另一个程序时,上述socketserver代码也会报告错误。

socket.error: [Errno 48] Address already in use

All my tests used the Python 2.7, Windows 7 and Centos 5. 我所有的测试都使用Python 2.7,Windows 7和Centos 5。

So my questions are why the HTTPServer will have this issue on windows? 所以我的问题是,为什么HTTPServer在Windows上会出现此问题? And how can I let my bottle programe will report the same error on windows, just like on windows? 以及如何让我的Bottle程序在Windows上报告相同的错误,就像在Windows上一样?

Sorry to bother all. 抱歉打扰了。

I've found the resolution, just so simple. 我已经找到解决方法,就这么简单。 Simply change the BaseHTTPServer.HTTPServer's attribute allow_reuse_address to 0. 只需将BaseHTTPServer.HTTPServer的属性allow_reuse_address更改为0。

The codes should be: 代码应为:

from bottle import route, run, template
import BaseHTTPServer

@route('/hello/:name')
def index(name='World'):
    return template('<b>Hello {{name}} </b>', name=name)

setattr(BaseHTTPServer.HTTPServer,'allow_reuse_address',0)
run(host='localhost', port=9999)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM