如何在AWS EC2 ubuntu实例中启动python BaseHttpserver

时间:2016-02-05 20:46:38

标签: python ubuntu amazon-ec2

我在我的本地ubuntu PC上运行以下HttpServer代码。但是当我尝试在aws ec2 ubuntu vm中运行此代码时,服务器无法启动。如何在ec2 ubuntu中实现以下功能?任何帮助/信息都会很棒

from BaseHTTPServer import BaseHTTPRequestHandler
import cgi
import os
import time
class PostHandler(BaseHTTPRequestHandler):

def SaveFile(self,file_data, fname, dirname):
    directory = '/home/user/Documents/TS/logs/'
    directory = directory + time.strftime("%Y-%m-%d")
    if not os.path.exists(directory):
            os.makedirs(directory)
    localfname = directory+'/'+fname
    with open(localfname,'wb') as f:
            f.write(file_data)

def do_POST(self):
    # Parse the form data posted
    form = cgi.FieldStorage(
        fp=self.rfile,
        headers=self.headers,
        environ={'REQUEST_METHOD':'POST',
                 'CONTENT_TYPE':self.headers['Content-Type'],
                 })

    # Begin the response
    self.send_response(200)
    self.end_headers()
    self.wfile.write('Client: %s\n' % str(self.client_address))
    self.wfile.write('User-agent: %s\n' % str(self.headers['user-agent']))
    self.wfile.write('Path: %s\n' % self.path)
    self.wfile.write('Form data:\n')

    # Echo back information about what was posted in the form
    for field in form.keys():
        field_item = form[field]
        if field_item.filename:
            # The field contains an uploaded file

            fname = form['filename'].value
            dirname = form['dirname'].value
            file_data = field_item.file.read()

            self.SaveFile(file_data, fname, dirname)
            file_len = len(file_data)
            del file_data
            self.wfile.write('\tUploaded %s as "%s" (%d bytes)\n' % \
                    (field, field_item.filename, file_len))
        else:
            # Regular form value
            self.wfile.write('\t%s=%s\n' % (field, form[field].value))
    return
if __name__ == '__main__':
from BaseHTTPServer import HTTPServer
server = HTTPServer(('locahost', 8080), PostHandler)

0 个答案:

没有答案