Python实现简单http服务器

作者:ordeder 时间:2022-02-04 03:55:01 

写一个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服务器

来源:http://blog.csdn.net/ordeder/article/details/22490373

标签:python,http,服务器
0
投稿

猜你喜欢

  • 不错的广告定位效果代码

    2009-06-05 18:51:00
  • Python 并行化执行详细解析

    2021-09-23 22:20:52
  • ASP 一次下载网页中的所有资源

    2008-04-18 13:04:00
  • ASP如何使用CDONTS来发送电子邮件?

    2010-06-05 12:35:00
  • Python中的进程操作模块(multiprocess.process)

    2022-09-17 23:10:32
  • CSS的未来:一些试验性CSS属性

    2011-06-10 13:20:00
  • Python实现二分查找算法实例

    2022-04-07 08:44:37
  • Python中str is not callable问题详解及解决办法

    2023-10-30 05:34:42
  • 使用PyCharm官方中文语言包汉化PyCharm

    2023-03-20 23:42:38
  • 操作Dom节点实现间歇滚动新闻

    2009-10-16 20:51:00
  • GO语言的IO方法实例小结

    2023-09-17 08:47:24
  • 利用Python自制网页并实现一键自动生成探索性数据分析报告

    2023-01-19 13:20:12
  • MySQL内建复制功能来优化可用性

    2010-10-25 20:20:00
  • pytorch VGG11识别cifar10数据集(训练+预测单张输入图片操作)

    2021-10-22 21:55:52
  • Python2.x与Python3.x的区别

    2022-03-24 18:36:46
  • 由浅入深讲解python中的yield与generator

    2022-08-14 06:26:11
  • matplotlib运行时配置(Runtime Configuration,rc)参数rcParams解析

    2022-08-31 12:17:15
  • python实现监控linux性能及进程消耗性能的方法

    2021-10-20 03:27:56
  • Django web自定义通用权限控制实现方法

    2021-07-31 06:21:09
  • asp DateDiff实现文字在特定时间后消失

    2011-03-11 11:11:00
  • asp之家 网络编程 m.aspxhome.com