免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2119 | 回复: 0
打印 上一主题 下一主题

python运行如下错误but no encoding declared [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-12-16 16:02 |只看该作者 |倒序浏览
文件格式utf-8

使用 源代码文件第一行添加:#coding:utf-8 也不行
  1. #-*- coding: UTF-8 -*-
  2. import os   #Python的标准库中的os模块包含普遍的操作系统功能  
  3. import re   #引入正则表达式对象  
  4. import urllib   #用于对URL进行编解码  
  5. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler  #导入HTTP处理相关的模块  
  6.   
  7.   
  8. #自定义处理程序,用于处理HTTP请求  
  9. class TestHTTPHandler(BaseHTTPRequestHandler):  
  10.     #处理GET请求  
  11.     def do_GET(self):  
  12.         #页面输出模板字符串  
  13.         templateStr = '''''   
  14. <html>   
  15. <head>   
  16. <title>QR Link Generator</title>   
  17. </head>   
  18. <body>   
  19. %s
  20. <br>   
  21. <br>   
  22. <form action="/qr" name=f method="GET"><input maxLength=1024 size=70   
  23. name=s value="" title="Text to QR Encode"><input type=submit   
  24. value="Show QR" name=qr>   
  25. </form>
  26. </body>   
  27. </html> '''  
  28.   
  29.   
  30.     # 将正则表达式编译成Pattern对象  
  31.     pattern = re.compile(r'/qr\?s=([^\&]+)\&qr=Show\+QR')  
  32.     # 使用Pattern匹配文本,获得匹配结果,无法匹配时将返回None  
  33.     match = pattern.match(self.path)  
  34.     qrImg = ''  
  35.          
  36.     if match:  
  37.         # 使用Match获得分组信息  
  38.         qrImg = '<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl=' + match.group(1) + '" /><br />' + urllib.unquote(match.group(1))   
  39.   
  40.     self.protocal_version = 'HTTP/1.1'  #设置协议版本  
  41.     self.send_response(200) #设置响应状态码  
  42.     self.send_header("Welcome", "Contect")  #设置响应头  
  43.     self.end_headers()  
  44.     self.wfile.write(templateStr % qrImg)   #输出响应内容  
  45.       
  46. #启动服务函数  
  47. def start_server(port):  
  48.     http_server = HTTPServer(('', int(port)), TestHTTPHandler)  
  49.     http_server.serve_forever() #设置一直监听并接收请求  
  50.   
  51. os.chdir('static')  #改变工作目录到 static 目录  
  52. start_server(8069)  #启动服务,监听8000端口
复制代码




您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP