Python实现简单http服务器


Posted in Python onApril 12, 2018

写一个python脚本,实现简单的http服务器功能:

1.浏览器中输入网站地址:172.20.52.163:20014

2.server接到浏览器的请求后,读取本地的index.html文件的内容,回发给浏览器

代码实现

server.py

#!/usr/bin/python 
import socket 
import signal 
import errno 
from time import sleep  
 
 
def HttpResponse(header,whtml): 
  f = file(whtml) 
  contxtlist = f.readlines() 
  context = ''.join(contxtlist) 
  response = "%s %d\n\n%s\n\n" % (header,len(context),context) 
  return response 
 
def sigIntHander(signo,frame): 
  print 'get signo# ',signo 
  global runflag 
  runflag = False 
  global lisfd 
  lisfd.shutdown(socket.SHUT_RD) 
 
strHost = "172.20.52.163" 
HOST = strHost #socket.inet_pton(socket.AF_INET,strHost) 
PORT = 20014 
 
httpheader = '''''\ 
HTTP/1.1 200 OK 
Context-Type: text/html 
Server: Python-slp version 1.0 
Context-Length: ''' 
 
lisfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
lisfd.bind((HOST, PORT)) 
lisfd.listen(2) 
 
signal.signal(signal.SIGINT,sigIntHander) 
 
runflag = True 
while runflag: 
  try: 
    confd,addr = lisfd.accept() 
  except socket.error as e: 
    if e.errno == errno.EINTR: 
      print 'get a except EINTR' 
    else: 
      raise 
    continue 
 
  if runflag == False: 
    break; 
 
  print "connect by ",addr 
  data = confd.recv(1024) 
  if not data: 
    break 
  print data 
  confd.send(HttpResponse(httpheader,'index.html')) 
  confd.close() 
else: 
  print 'runflag#',runflag 
 
print 'Done'

index.html

<html> 
 <head> 
   <title>Python Server</title> 
 </head> 
 <body> 
  <h1>Hello python</h1> 
  <p>Welcom to the python world</br> 
 </body> 
</html>

测试

测试结果:

root@cloud2:~/slp/pythonLearning/socket# ./server_v1.py 
connect by ('172.20.52.110', 6096)
GET / HTTP/1.1
Host: 172.20.52.163:20014
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en;q=0.6

浏览器

Python实现简单http服务器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中的__SLOTS__属性使用示例
Feb 18 Python
Python中的列表知识点汇总
Apr 14 Python
用于统计项目中代码总行数的Python脚本分享
Apr 21 Python
Python实现读取txt文件并画三维图简单代码示例
Dec 09 Python
python批量替换多文件字符串问题详解
Apr 22 Python
python字符串string的内置方法实例详解
May 14 Python
python 用正则表达式筛选文本信息的实例
Jun 05 Python
python GUI实现小球满屏乱跑效果
May 09 Python
python3+PyQt5 自定义窗口部件--使用窗口部件样式表的方法
Jun 26 Python
Python 2.6.6升级到Python2.7.15的详细步骤
Dec 14 Python
详解python使用金山词霸的翻译功能(调试工具断点的使用)
Jan 07 Python
在 Golang 中实现 Cache::remember 方法详解
Mar 30 Python
Python实现一个服务器监听多个客户端请求
Apr 12 #Python
python使用socket创建tcp服务器和客户端
Apr 12 #Python
Python简单实现两个任意字符串乘积的方法示例
Apr 12 #Python
Java与Python两大幸存者谁更胜一筹呢
Apr 12 #Python
python打包压缩、读取指定目录下的指定类型文件
Apr 12 #Python
pandas全表查询定位某个值所在行列的方法
Apr 12 #Python
Python Pandas找到缺失值的位置方法
Apr 12 #Python
You might like
用PHP调用数据库的存贮过程!
2006/10/09 PHP
php代码中使用换行及(\n或\r\n和br)的应用
2013/02/02 PHP
PHP生成二维码的两个方法和实例
2014/07/01 PHP
php 批量查询搜狗sogou代码分享
2015/05/17 PHP
PHP mysqli_free_result()与mysqli_fetch_array()函数详解
2016/09/21 PHP
PHP实现对文件锁进行加锁、解锁操作的方法
2017/07/04 PHP
laravel-admin 管理平台获取当前登陆用户信息的例子
2019/10/08 PHP
window.onbeforeunload方法在IE下无法正常工作的解决办法
2010/01/23 Javascript
通过javascript的匿名函数来分析几段简单有趣的代码
2010/06/29 Javascript
JavaScript 验证码的实例代码(附效果图)
2013/03/22 Javascript
jquery解析xml字符串示例分享
2014/03/25 Javascript
jQuery中:focus选择器用法实例
2014/12/30 Javascript
jQuery链使用指南
2015/01/20 Javascript
JavaScript常用基础知识强化学习
2015/12/09 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
JavaScript知识点总结(十六)之Javascript闭包(Closure)代码详解
2016/05/31 Javascript
Bootstrap源码解读表单(2)
2016/12/22 Javascript
AngularJS改变元素显示状态
2017/04/20 Javascript
使用vue-cli导入Element UI组件的方法
2018/05/16 Javascript
php中and 和 &amp;&amp;出坑指南
2018/07/13 Javascript
JS实现可视化文件上传
2018/09/08 Javascript
微信小程序实现一张或多张图片上传(云开发)
2019/09/25 Javascript
微信小程序学习总结(一)项目创建与目录结构分析
2020/06/04 Javascript
浅谈vue-props的default写不写有什么区别
2020/08/09 Javascript
[00:55]2015国际邀请赛中国区预选赛5月23日——28日约战上海
2015/05/25 DOTA
深入理解python函数递归和生成器
2016/06/06 Python
Python网络编程中urllib2模块的用法总结
2016/07/12 Python
Python实现的十进制小数与二进制小数相互转换功能
2017/10/12 Python
关于python列表增加元素的三种操作方法
2018/08/22 Python
如何获取Python简单for循环索引
2019/11/21 Python
python golang中grpc 使用示例代码详解
2020/06/03 Python
python爬虫把url链接编码成gbk2312格式过程解析
2020/06/08 Python
详解通过变换矩阵实现canvas的缩放功能
2019/01/14 HTML / CSS
2013年学期结束动员演讲稿
2014/01/07 职场文书
Pyhton爬虫知识之正则表达式详解
2022/04/01 Python
MySQL 执行数据库更新update操作的时候数据库卡死了
2022/05/02 MySQL