下面是我目前使用的代码:
#! /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()
该代码在本地主机上按预期工作,但在公共服务器上无响应。
此外,执行代码两次会导致以下错误消息:
错误:(98,'地址已在使用')