gpt4 book ai didi

Python SocketServer 适用于本地主机但不适用于服务器

转载 作者:太空狗 更新时间:2023-10-30 02:13:04 27 4
gpt4 key购买 nike

下面是我目前使用的代码:

#! /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()

代码在本地主机上按预期工作,但在公共(public)服务器上没有响应。

此外,执行代码两次会导致以下错误消息:

error: (98, 'Address already in use')

最佳答案

error: (98, 'Address already in use')

为此你需要这个:

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

but is unresponsive on public server.

通常在共享主机的情况下,您无法通过创建套接字来完成。在任何情况下,您都可以尝试以下操作,看看是否有帮助:

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

关于Python SocketServer 适用于本地主机但不适用于服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11297875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com