python3 使用时如下:

#!/usr/bin/env python3
#coding=utf-8
from http.server import SimpleHTTPRequestHandler
import socketserver
import os,io,shutil
import logging
import cgi
import sys
import json log_path = './logs/run_server_logs.log'
logging.basicConfig(level=logging.INFO,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S',filename=log_path)
class MyHttpHandler(SimpleHTTPRequestHandler): def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers() def send_datas(self,contents):
#指定返回编码
enc = "UTF-8"
content = contents.encode(enc)
f = io.BytesIO()
f.write(content)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(contents)))
self.end_headers()
shutil.copyfileobj(f,self.wfile) def do_GET(self):
logging.info("got get request "+str(self.path))
values = str(self.path)
self.send_datas('这是get请求'+values) def do_POST(self):
logging.info("got post!!")
datasets = cgi.FieldStorage(fp = self.rfile,headers = self.headers,environ = {'REQUEST_METHOD': 'POST'})
logging.info(str(datasets))
id = datasets.getvalue('id')
name = datasets.getvalue('name')
msg = "name=="+str(name)+" id=="+str(id)
flag = 1
results = {"status":flag,"msg":msg}
self.send_datas(json.dumps(results)) def start_server():
server_host = '127.0.0.1'
server_port = 8080
httpd = socketserver.TCPServer((server_host,server_port), MyHttpHandler)
logging.info('\nStart server success ... \nserver_host:'+server_host+' server_port:'+str(server_port))
print('exe_server started on '+str(server_host)+' server_port:'+str(server_port))
httpd.serve_forever() if __name__ == "__main__":
start_server()

python2使用时:

#!/usr/bin/env python
#coding=utf-8
from SimpleHTTPServer import SimpleHTTPRequestHandler
import SocketServer
import os,io,shutil
import logging
import cgi
import urlparse
import sys
reload(sys)
sys.setdefaultencoding('utf-8') log_path = './logs/run_server_logs.log'
logging.basicConfig(level=logging.INFO,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S',filename=log_path)
class MyHttpHandler(SimpleHTTPRequestHandler): def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers() def send_datas(self,contents):
#指定返回编码
enc = "UTF-8"
#contents = contents.encode(enc)
f = io.BytesIO()
f.write(contents)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(contents)))
self.end_headers()
shutil.copyfileobj(f,self.wfile) def do_GET(self):
logging.info("got get request "+str(self.path))
values = str(self.path)
self.send_datas('get请求方式'+values) def do_POST(self):
logging.info("got post!!")
datasets = cgi.FieldStorage(fp = self.rfile,headers = self.headers,environ = {'REQUEST_METHOD': 'POST'})
logging.info(str(datasets))
id = datasets.getvalue('id')
name = datasets.getvalue('name') msg = "name=="+str(name)+" id=="+str(id)
flag = 1
results = {'status':flag,'msg':msg}
self.send_datas(str(results)) def start_server():
server_host = '127.0.0.1'
server_port = 8080
httpd = SocketServer.TCPServer((server_host,server_port), MyHttpHandler)
logging.info('\nStart server success ... \nserver_host:'+server_host+' server_port:'+str(server_port))
print('exe_server started on '+str(server_host)+' server_port:'+str(server_port))
httpd.serve_forever() if __name__ == "__main__":
start_server()

python3与python2中SimpleHTTPRequestHandler导入方式不同,3是 from http.server import SimpleHTTPRequestHandler  , 2是 from SimpleHTTPServer import SimpleHTTPRequestHandler  ;

另外serversocket也不一样3是 import socketserver httpd = socketserver.TCPServer((server_host,server_port), MyHttpHandler) ,2是 import SocketServer httpd = SocketServer.TCPServer((server_host,server_port), MyHttpHandler)

其他变化可自行设置.......

python3与python2使用python原生SimpleHTTPRequestHandler的更多相关文章

  1. Win10下python3和python2同时安装并解决pip共存问题

    特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似. 使用python开发,环境有Python2和 python3 两种,有时候需要两种环境切换使用,下 ...

  2. 【转】Win10下 python3和python2同时安装并解决pip共存问题

    1.下载python3和python2 进入python官网,链接https://www.python.org/ 选择Downloads--->Windows,点击进入就可以看到寻找想要的pyt ...

  3. 【转】Win10下python3和python2多版本同时安装并解决pip共存问题

    [转]Win10下python3和python2多版本同时安装并解决pip共存问题 特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似. 使用pytho ...

  4. Windows同时安装python3和python2

    Windows同时安装python3和python2 https://www.cnblogs.com/shanhua-fu/p/6912683.html Windows7 下python3和pytho ...

  5. Windows7 下python3和python2同时 安装python3和python2

    1.下载python3和python2 进入python官网,链接https://www.python.org/ 选择Downloads--->Windows,点击进入就可以看到寻找想要的pyt ...

  6. Windows下python3和python2同时安装python2.exe、python3.exe和pip2、pip3设置

    1.添加python2到系统环境变量 打开,控制面板\系统和安全\系统,选择高级系统设置,环境变量,选择Path,点击编辑,新建,分别添加D:\Python\python27和D:\Python\py ...

  7. 从零开始学习PYTHON3讲义(一)认识Python

    课程名称 从零开始PYTHON3 课程长度 15讲 适用年龄 15-20岁(初三-大一) 本讲名称 认识Python 时长 90分钟 教学内容分析 Python是时下最流行的计算机编程语言之一.本课程 ...

  8. 一、Windows10下python3和python2同时安装

    python2.exe.python3.exe和pip2.pip3设置 说明:安装安装python3和python2请参考本系列教程(一) 1.添加python2到系统环境变量 打开,控制面板\系统和 ...

  9. ubuntu python3和python2切换脚本

    最近在ubuntu上开发较多,有些工具只能在python2运行,而开发又是在python3上做的开发,所以写个脚本方便在python2和python3之间切换. 切换成python2的文件usepy2 ...

随机推荐

  1. MySQL面试题36道

    MySQL数据库是在免费的数据库中最受欢迎的一款,尤其是在一些小型项目以及项目资金有限的情况下,选择MySQL来作为数据存储的工具,那些不差钱并且数据吞吐量非常大的互联网公司一般都是会用付费的Orac ...

  2. Jenkins 基于 Docker git JAVA CI/CD

    准备两台机器 192.168.31.200 centos7  docker harbor git 192.168.31.201 centos7  docker jenkins maven git Ha ...

  3. 面向对象程序设计__Task6_Calculator1.6.2

    The 4th part of the Calculator program _ Interface 题目链接:第六次作业(计算器第四步) github链接:Calculator_1.6.2 第六次作 ...

  4. webstorm 搭建es6开发环境

    本文转自:http://www.jianshu.com/p/26601581e152 1:新建一个Empty Project项目es6 ,然后在src目录下新建了一个es.js: 2:打开设置pref ...

  5. 添加外键式异常 1215-cannot add foreign key constranint

    添加外键时报错,原因是添加外键的表的字段的字段类型不一致 比如我的第一张表id是int类型,添加约束的dep_id是bigint类型,所以报错,只要把两张表添加约束的字段类型改成统一的即可 本人大学生 ...

  6. VC++编译错误error C2065: “HANDLE”: 未声明的标识符及添加winbase.h后提示winbase.h(243): error C2146: 语法错误: 缺少“;”(在标识符“Internal”的前面)的解决办法

    问题描述: VC++程序编译时提示错误:error C2065: “HANDLE”: 未声明的标识符等众多错误提示,如下所示: error C2065: “HANDLE”: 未声明的标识符 error ...

  7. C#控件中的KeyDown、KeyPress 与 KeyUp事件浅谈

    研究了一下KeyDown,KeyPress 和 KeyUp 的学问.让我们带着如下问题来说明: 1.这三个事件的顺序是怎么样的? 2.KeyDown 触发后,KeyUp是不是一定触发? 3.三个事件的 ...

  8. 无oracle客户端仅用plsql连接远程oracle

    1.在安装ORACLE服务器的机器上搜索下列文件,oci.dllocijdbc10.dllociw32.dllorannzsbb10.dlloraocci10.dlloraociei10.dllsql ...

  9. Vue表单绑定(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  10. JQuery radio单选框应用

    转载:JQuery判断radio(单选框)是否选中和获取选中值方法总结 一.利用获取选中值判断选中 直接上代码,别忘记引用JQuery包 复制代码 代码如下: < !DOCTYPE html P ...