简体   繁体   English

Python SocketServer适用于localhost,但不适用于服务器

[英]Python SocketServer works on localhost but not on server

Included below is the code that I am currently using: 下面是我目前使用的代码:

#! /usr/bin/python
print 'Content-type: application'
print '\n\n'

import SocketServer
import cgitb
cgitb.enable()

class MyTCPHandler(SocketServer.BaseRequestHandler):
    """
    The RequestHandler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    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())
        self.request.sendall('Data Received')

if __name__ == "__main__":
    HOST, PORT = "localhost", 9989

    # Create the server, binding to localhost on port 9989
    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 code works as expected on localhost, but is unresponsive on public server. 代码在localhost上按预期工作,但在公共服务器上没有响应。

In addition, executing the code twice results in the following error message: 此外,执行代码两次会导致以下错误消息:

error: (98, 'Address already in use') 错误:(98,'地址已在使用中')

I think the problem is that you are binding to "localhost" , ie, on the loopback interface. 我认为问题是你绑定到"localhost" ,即在loopback接口上。 Try replacing "localhost" with the public IP address you want to bind to. 尝试将"localhost"替换为要绑定到的公共IP地址。 Type ifconfig at the command line if you're not sure what that is; 如果您不确定那是什么,请在命令行键入ifconfig; pick the address that's not in any one of the IP blocks designated for private use (ie, doesn't start with 10. or 192.168. etc). 选择不在指定供私人使用的任何一个IP块中的地址(即,不以10.或192.168等开头)。

I'm not sure if it will work with TCPServer specifically but often software that requires you to bind to a specific interface will accept "0.0.0.0" for all interfaces, or an empty string to the same effect. 我不确定它是否适用于TCPServer,但通常需要绑定到特定接口的软件对所有接口都接受"0.0.0.0" ,或者对相同的效果接受空字符串。

error: (98, 'Address already in use') 错误:(98,'地址已在使用中')

You need this for that: 你需要这个:

socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

but is unresponsive on public server. 但在公共服务器上没有响应。

Usually in case of shared hosting, you cant get through with creating a socket. 通常在共享托管的情况下,您无法通过创建套接字来完成。 In any case you could try the following, to see if it helps: 在任何情况下,您都可以尝试以下方法,看看它是否有帮助:

HOST, PORT = "", 9989 # or (public_IP,9989)

You have to use "localhost" by gethostbyname() function: 你必须使用gethostbyname()函数的"localhost"

server = SocketServer.TCPServer((socket.gethostbyname(HOST), PORT), MyTCPHandler)

But you have to keep in mind that some machines doesn't understand "localhost" and you have to use the local host IP address , 127.0.0.1 instead. 但是你必须记住,有些机器不理解"localhost" ,你必须使用本地主机IP地址127.0.0.1

You can't run it twice because you have a static port address (9989). 由于您有静态端口地址(9989),因此无法运行它两次。 There can only be one listener bound to a port. 只能有一个绑定到端口的侦听器。 There can be more than one incoming connection to the port, but only one listener. 可以有多个到端口的传入连接,但只有一个侦听器。

Also, have you checked the firewall settings. 您还检查了防火墙设置吗? Wherever your python server is running, the firewall has to give port 9989 permission to accept incoming connections. 无论您的python服务器在哪里运行,防火墙都必须授予端口9989接受传入连接的权限。 Also, if your server is behind a hub, the hub has to be told which host will be handling port 9989. 此外,如果您的服务器位于集线器后面,则必须告知集线器哪个主机将处理端口9989。

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

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