下面是我目前使用的代码:
#! /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__": …Run Code Online (Sandbox Code Playgroud)