返回用户指定页面的web服务器
import socket
import re
import os def handle_client(socket_con):
"""
接收来自客户端的请求,并接收请求报文,解析,返回
"""
# 1、服务器接收客户端的请求报文
request = socket_con.recv(4096).decode()
# 以行切割请求报文为列表
res = request.split('\r\n')
# 取第一位(请求行):GET / HTTP/1.1,并用正则切割GET / HTTP/1.1,取出路径位置
path = re.match('\w+\s(\S+)',res[0])
path = path.group(1)
# 判断路径长度,大于一则拼接出路径,小于等于一则显示首页
if len(path) > 1:
# 路径取出,开始拼接资源路径(绝对路径自己填写)
path = '# 文件夹绝对路径' + path
print(path)
else:
# 显示首页代码
response_line = 'HTTP/1.1 200 OK\r\n'
response_head = 'Content-Type:text/html;charset=utf-8\r\n'
response_body = '''
<html>
<head>
<title>首页</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>首页</h1>
<p>欢迎来到首页</p>
<p><em>感谢你的使用</em></p>
</body>
</html>'''
response = response_line + response_head + '\r\n' + response_body
socket_con.send(response.encode())
socket_con.close()
# 路径大于一并取出之后判断资源是否存在
if not os.path.exists(path):
# 资源不存在则显示资源不存在界面
response_line = 'HTTP/1.1 404 NOT FOUND\r\n'
response_head = 'Content-Type:text/html;charset=utf-8\r\n'
response_body = '''
<html>
<head>
<title>错误</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>你请求的资源不存在!</h1>
<p>如果你想访问一个资源,请输入正确的资源路径</p>
<p><em>感谢你的使用</em></p>
</body>
</html>'''
response = response_line + response_head + '\r\n' + response_body
socket_con.send(response.encode())
socket_con.close()
return
else:
# 资源存在以后判断是否是文件,是文件则直接读取
if os.path.isfile(path):
response_line = 'HTTP/1.1 200 OK\r\n'
response_head = 'Server:skylark 2.0\r\n'
response_head += 'Content-Type:*/*;charset:utf-8\r\n'
f = open(path, 'rb')
response_body = f.read()
response = response_line.encode() + response_head.encode() + '\r\n'.encode() + response_body
socket_con.send(response)
socket_con.close()
return
else:
if path.endswith('/'):
# 如果是文件夹
# 判断文件夹下是否有默认文件,如果有则返回,如果没有则判断服务器是否开启了目录浏览
# 默认文件:index.html default.html
# 是否可以访问默认文件开关,True 开 ,False 关
default_document = False
if default_document:
# 判断用户访问的文件夹下是否有index.html 或者 default.html
if os.path.exists(path + 'index.html'):
response_line = 'HTTP/1.1 200 OK\r\n'
response_head = 'Server:skylark 2.0\r\n'
response_head += 'Content-Type:*/*;charset:utf-8\r\n'
f = open(path + 'index.html', 'rb')
response_body = f.read()
response = response_line.encode() + response_head.encode() + '\r\n'.encode() + response_body
socket_con.send(response)
socket_con.close()
return
elif os.path.exists(path + 'default.html'):
response_line = 'HTTP/1.1 200 OK\r\n'
response_head = 'Server:skylark 2.0\r\n'
response_head += 'Content-Type:*/*;charset:utf-8\r\n'
f = open(path + 'default.html', 'rb')
response_body = f.read()
response = response_line.encode() + response_head.encode() + '\r\n'.encode() + response_body
socket_con.send(response)
socket_con.close()
return
else:
# 如果没有上述两个页面,则可以返回404错误,或者302重定向
response_line = "HTTP/1.1 404 Not Found\r\n"
response_head = "Server:skylark 2.0\r\n"
response_body = "index.html or default.html is not exist!!!"
response = response_line + response_head + "\r\n" + response_body
socket_con.send(response.encode())
socket_con.close()
# 不能访问默认文件情况下,判断服务器是否开启了目录浏览
else:
dir_browsing = True
if dir_browsing:
# 把用户请求的文件夹中所有的文件和文件夹以目录的形式返回到页面中
# 获取用户请求的文件夹
list_names = os.listdir(path)
response_line = 'HTTP/1.1 200 OK\r\n'
response_head = 'Server:skylark 2.0\r\n'
# 动态的拼接页面,将目录中的文件或者文件夹的名称以HTML页面的方式返回给浏览器
response_body = '<html><head><body><ul>'
for item in list_names:
response_body += "<li><a href = '#'>" + item + "</a></li>"
response_body += '</ul></body></head></html>'
response = response_line + response_head + "\r\n" + response_body
socket_con.send(response.encode())
socket_con.close()
return
else:
# 用户请求的路径没有斜线
# 重定向到+斜线的目录下,并显示重定向以后的路径(此处可以增加有斜线目录处理方式也就是上面的方法)
response_line = 'HTTP/1.1 302 Found\r\n'
response_head = 'Server:skylark 2.0\r\n'
response_head += 'Content-Type:text/html;charset=utf-8\r\n'
response_body = '重定向' + path + '/'
response = response_line + response_head + '\r\n' + response_body
socket_con.send(response.encode())
socket_con.close() def main():
# 1、服务器创建负责监听的socket
socket_watch = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# 2、设置地址重用
socket_watch.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 3、绑定监听的端口
socket_watch.bind(("",8888))
# 4、设置监听队列
socket_watch.listen(128)
# 5、通过循环,不停的接收来自客户端的连接请求
while True:
socket_con,con_adds = socket_watch.accept()
# 注意将con_adds转成字符串
print("客户端:%s连接成功!!!" % str(con_adds))
# 接收来自客户端的请求,并接收请求报文,解析,返回
handle_client(socket_con) if __name__ == '__main__':
main()
返回用户指定页面的web服务器的更多相关文章
- 返回固定页面的web服务器
import socket def handle_client(socket_con): """ 接收来自客户端的请求,并接收请求报文,解析,返回 "" ...
- 一个简单的NodeJs静态页面的web服务器
主要功能 1 显示www文件夹下静态html或文本类型的文件. 2 缺省访问文件功能. 通过config.js的defaultfile属性设置 3 如果文件夹下没有缺省文件,显示文件夹下文件列表 4 ...
- 通过Web Api 和 Angular.js 构建单页面的web 程序
通过Web Api 和 Angular.js 构建单页面的web 程序 在传统的web 应用程序中,浏览器端通过向服务器端发送请求,然后服务器端根据这个请求发送HTML到浏览器,这个响应将会影响整个的 ...
- 域名ip自动跳转 跳转指定页面的js
域名ip自动跳转 跳转指定页面的js 为了应对百度审核,需要客户的网站在个别地区跳转到另一个页面,就搞到了这段代码,屡试不爽,超实用.下面把地址换成你要访问的网站url地址或者文件url地址即可.超实 ...
- 微信公众号页面的web页面键盘弹起问题
今天开发的过程中,遇到了一个小问题,是这样的, UI的设计稿中有个底部的按钮是相对于屏幕定位的,但是这个页面还有一个输入框:具体情况请看下图: 这就造成了当我们输入框获取焦点的时候,键盘弹起,下面的 ...
- Web服务器-服务器开发-返回固定页面的HTTP服务器(3.3.1)
@ 目录 1.注意 2.代码 关于作者 1.注意 浏览器解析的时候偶\r\n才算一个换行符 发送的str要编码,这里使用的是utf8 其他的都和上一篇没有什么区别 这里主要返回的是固定的网址 2.代码 ...
- node.js连接数据库登录注册,修改用户(页面的ajax请求)
首先给大家看一下目录结构,结构如下: index.html 首页(显示所有的用户信息) login.html 登录页 register.html 注册页 db.js 配置链接数据库参数 dbhelpe ...
- 一个简单的定向python爬虫爬取指定页面的jpg图片
import requests as r import re resul=r.get("http://www.imooc.com/course/list") urlinfo=re. ...
- 闲来无聊,研究一下Web服务器 的源程序
web服务器是如何工作的 1989年的夏天,蒂姆.博纳斯-李开发了世界上第一个web服务器和web客户机.这个浏览器程序是一个简单的电话号码查询软件.最初的web服务器程序就是一个利用浏览器和web服 ...
随机推荐
- 读<<programming ruby>> 7.6节 flip-flop 理解
书中源码是这样的 File.foreach('1.txt') do |x| if(($. == 1) || x =~ /eig/) .. (($. == 3) || x =~ /nin/) then ...
- 《nginx 三》实现nginx的动态负载均衡——实战
Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...
- java多线程通过管道流实现不同线程之间的通信
java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据.通过使用管道,实现不同线程间的通信,而不必借助类似 ...
- pat04-树7. Search in a Binary Search Tree (25)
04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...
- Python实现抓取CSDN热门文章列表
1.使用工具: Python3.5 BeautifulSoup 2.抓取网站: csdn热门文章列表 http://blog.csdn.net/hot.html 3.分析网站代码: 4.实现代码: _ ...
- [Java][Liferay] 模拟用户
以admin的帐号登陆 Navigation to Users and Organizations -> All Users 找到你要查看的user,点击Actions->Imperson ...
- DBCP数据库连接池原理分析
在比较大的项目中,需要不断的从数据库中获取数据,Java中则使用JDBC连接数据库,但是获取数据库的连接可是相当耗时的操作,每次连接数据库都获得 .销毁数据库连接,将是很大的一个开销.为了解决这种开销 ...
- angular的多个模块执行 angular里 字符串与对象的互转
1.disable : true ,禁用 2.$timeout 计时器 $interval.cancel(timer); 3.app.run(); 可以不使用控制器就开启数据,但适合$rootsco ...
- Android Process & Thread
Native Service and Android Service Native Service:In every main() method of NativeService, which is ...
- Azure本月最新活动,速度Mark!!
缤纷五月,翠色盈盈,风光如画,小编在这里给大家汇总了这个多彩五月最新的活动合集.我们一切都准备好了,就等你来参加了~ 首先最重磅的当然是新一届的全球微软开发者大会! 有吃有喝有 Build,5 月 ...