Python之最简单的http的echo 服务

本文介绍了如何使用Python创建一个最简单的HTTP服务器,该服务器能够响应请求并返回接收到的数据,即HTTP Echo服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

## 最简http服务器模型

import os, sys, platform
import BaseHTTPServer
from SocketServer import ThreadingMixIn
import threading
import urllib, urllib2
import shutil
import re
import time
try:
  from cStringIO import StringIO
except ImportError:
  from StringIO import StringIO

########################################################################################################
#   Overload and Impl the HttpRequest Handler.
########################################################################################################
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  #[GET]
  def do_GET(self):
    f = StringIO()
    f.write(self.path)
    length = f.tell()
    f.seek(0)
    self.response(length)
    if f:
      shutil.copyfileobj(f, self.wfile)
      f.close()
  #[Response]
  def response(self,length):
      self.send_response(200)
      self.send_header("Content-type", "text/html")
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值