Python實(shí)現(xiàn)簡(jiǎn)單http服務(wù)器
寫一個(gè)python腳本,實(shí)現(xiàn)簡(jiǎn)單的http服務(wù)器功能:
1.瀏覽器中輸入網(wǎng)站地址:172.20.52.163:20014
2.server接到瀏覽器的請(qǐng)求后,讀取本地的index.html文件的內(nèi)容,回發(fā)給瀏覽器
代碼實(shí)現(xiàn)
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>
測(cè)試
測(cè)試結(jié)果:
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
瀏覽器

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python內(nèi)置的HTTP協(xié)議服務(wù)器SimpleHTTPServer使用指南
- 使用Python來(lái)編寫HTTP服務(wù)器的超級(jí)指南
- 用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的能夠上傳下載的HTTP服務(wù)器
- python創(chuàng)建一個(gè)最簡(jiǎn)單http webserver服務(wù)器的方法
- 使用nodejs、Python寫的一個(gè)簡(jiǎn)易HTTP靜態(tài)文件服務(wù)器
- Python實(shí)現(xiàn)簡(jiǎn)單的HttpServer服務(wù)器示例
- Python搭建HTTP服務(wù)器和FTP服務(wù)器
- Python通過(guò)命令開啟http.server服務(wù)器的方法
- python探索之BaseHTTPServer-實(shí)現(xiàn)Web服務(wù)器介紹
- python實(shí)現(xiàn)簡(jiǎn)單http服務(wù)器功能
相關(guān)文章
Python使用pyyaml模塊處理yaml數(shù)據(jù)
這篇文章主要介紹了Python使用pyyaml模塊處理yaml數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
解決pandas展示數(shù)據(jù)輸出時(shí)列名不能對(duì)齊的問(wèn)題
今天小編就為大家分享一篇解決pandas展示數(shù)據(jù)輸出時(shí)列名不能對(duì)齊的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
python?NetworkX庫(kù)生成并繪制帶權(quán)無(wú)向圖
這篇文章主要為大家介紹了python?NetworkX庫(kù)生成并繪制帶權(quán)無(wú)向圖的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python模塊結(jié)構(gòu)與布局操作方法實(shí)例分析
這篇文章主要介紹了Python模塊結(jié)構(gòu)與布局操作方法,結(jié)合實(shí)例形式分析了Python模塊與布局的相關(guān)概念、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
pandas 轉(zhuǎn)換成行列表進(jìn)行讀取與Nan處理的方法
今天小編就為大家分享一篇pandas 轉(zhuǎn)換成行列表進(jìn)行讀取與Nan處理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
PyCharm無(wú)法調(diào)用numpy(報(bào)錯(cuò)ModuleNotFoundError:No?module?named?&a
本文主要介紹了PyCharm無(wú)法調(diào)用numpy(報(bào)錯(cuò)ModuleNotFoundError:No?module?named?'numpy'),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python程序員開發(fā)中常犯的10個(gè)錯(cuò)誤
這篇文章主要介紹了Python程序員開發(fā)中常犯的10個(gè)錯(cuò)誤,不知道你有沒有中槍呢,需要的朋友可以參考下2014-07-07

