【HeadFirst】第七章:web开发[本地localhost访问][自动访问index.html]
来源:互联网 发布:学编程怎么开始学 编辑:程序博客网 时间:2024/05/22 10:58
在浏览器中输入:http://localhost:8080
而不是https://localhost:8080,会出现报错
启动simple_httpd.py,访问上述网址,为什么会自动访问到index.html?而不是其他py文件?
# coding=gbk'''提供一个支持CGI的web服务器'''from http.server import HTTPServer, CGIHTTPRequestHandlerport = 8080httpd = HTTPServer(('', port), CGIHTTPRequestHandler)print("Starting simple_httpd on port: " + str(httpd.server_port))httpd.serve_forever()
看源代码:
\Python 3.5\Lib\http\server.py
def send_head(self): """Common code for GET and HEAD commands. This sends the response code and MIME headers. Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances), or None, in which case the caller has nothing further to do. """ path = self.translate_path(self.path) f = None if os.path.isdir(path): parts = urllib.parse.urlsplit(self.path) if not parts.path.endswith('/'): # redirect browser - doing basically what apache does self.send_response(HTTPStatus.MOVED_PERMANENTLY) new_parts = (parts[0], parts[1], parts[2] + '/', parts[3], parts[4]) new_url = urllib.parse.urlunsplit(new_parts) self.send_header("Location", new_url) self.end_headers() return None for index in "index.html", "index.htm":#================== 此处 =================== index = os.path.join(path, index) if os.path.exists(index): path = index break else:#=============== 奇怪的缩进 位置相对于上面的for和两个if================= return self.list_directory(path) ctype = self.guess_type(path) try: f = open(path, 'rb') except OSError: self.send_error(HTTPStatus.NOT_FOUND, "File not found") return None try: self.send_response(HTTPStatus.OK) self.send_header("Content-type", ctype) fs = os.fstat(f.fileno()) self.send_header("Content-Length", str(fs[6])) self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) self.end_headers() return f except: f.close() raise
0 0
- 【HeadFirst】第七章:web开发[本地localhost访问][自动访问index.html]
- 局域网访问本地localhost
- android 真机 访问本地web服务器 localhost login 注意事项
- android 真机 访问本地web服务器 localhost
- nodejs访问本地静态资源(进入index.html)
- 通过 localhost 不能访问本地服务器问题
- android 访问本地站点 localhost 127.0.0.1
- android调试访问localhost与本地服务
- Android模拟器访问本地服务器 localhost
- NSMutableURLRequest 访问web html
- Android模拟器访问本地tomcat服务器下的web应用(webservice),localhost换成10.0.2.2
- HTML 访问本地 Markdown 文件
- 域名需要添加/index.html才能访问
- apache 指定访问index.html文件
- DedeCMS访问网站首页会自动在域名后面多出index.html的解决方法
- android访问本地Web服务器
- FAQ,本地访问web工程
- 关于iis的本地访问http://localhost ,http404 , http500错误
- 1-1、Spark 的local模式安装
- Java中Vector和ArrayList的区别
- UNIX环境高级编程之创建进程与线程
- jQuery中prop()和attr()方法的测试和总结
- JSP语法
- 【HeadFirst】第七章:web开发[本地localhost访问][自动访问index.html]
- TCP/IP详解卷1 读书笔记:第二十八章 SMTP和POP3协议
- nginx + gunicorn + virtualenv部署django
- 导航栏的背景为不透明
- Java编写的重命名工具
- ITOO讨论会
- g++编译c++文件
- ARMv8 與 Linux的新手筆記
- Maven学习系列--03进一步了解Maven