Python3开启http服务的方式是什么,哪些事项要注意
Admin 2022-07-22 群英技术资讯 1828 次浏览
这篇文章给大家分享的是Python3开启http服务的方式是什么,哪些事项要注意。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。Python中自带了简单的服务器程序,能较容易地打开服务。
在python3中将原来的SimpleHTTPServer命令改为了http.server,使用方法如下:
1. cd www目录
2. python -m http.server
开启成功,则会输出“Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) …”,表示在本机8000端口开启了服务。
如果需要后台运行,可在命令后加"&"符号,Ctrl+C不会关闭服务,如下:
python -m http.server &
如果要保持服务,则在命令前加nohup以忽略所有挂断信号,如下:
nohup python -m http.server 8001
如果不使用默认端口,可在开启时附带端口参数,如:
python -m http.server 8001
则会在8001端口打开http服务。
可以使用http://0.0.0.0:8000/查看www目录下的网页文件,若无index.html则会显示目录下的文件。
也可以使用ifconfig命令查看本机IP并使用。
补充:python创建http服务
用java调用dll的时候经常出现 invalid memory access,改用java-Python-dll,
Python通过http服务给java提供功能。
Python3.7
通过 http.server.BaseHTTPRequestHandler 来处理请求,并返回response
filename为输入日志名称,默认是同目录下,没有该文件会新创建
filemode a 是追加写的模式,w是覆盖写
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
filename="hhh.txt",
filemode='a'
)
logging.info("xxxx")
pchar - ctypes.c_char_p
integer 用了 bytes(0),byref(ctypes.c_void_p(0)) 都OK,没有更深入去研究,如有错误请指正。
import ctypes
from ctypes import *
dll = ctypes.windll.LoadLibrary('C:\\xxx\\xxx.dll')
print("dll版本号为 : "+ str(dll.GetVersion()) )
name = ctypes.c_char_p(b"gc")
roomno = ctypes.c_char_p(bytes(room.encode("utf-8")))
begintime = ctypes.c_char_p(bytes(begin.encode("utf-8")))
endtime = ctypes.c_char_p(bytes(end.encode("utf-8")))
cardno = ctypes.c_void_p(0)
dll.invoke...
要注意 必须有 response = response_start_line + response_headers + “\r\n” + response_body
拼接应答报文后,才能给浏览器正确返回
# coding:utf-8
import socket
from multiprocessing import Process
def handle_client(client_socket):
# 获取客户端请求数据
request_data = client_socket.recv(1024)
print("request:", request_data)
# 构造响应数据
response_start_line = "HTTP/1.1 200 OK\r\n"
response_headers = "Server: My server\r\n"
response_body = "helloWorld!"
response = response_start_line + response_headers + "\r\n" + response_body
print("response:", response)
# 向客户端返回响应数据
client_socket.send(bytes(response, "utf-8"))
# 关闭客户端连接
client_socket.close()
if __name__ == "__main__":
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 8888))
server_socket.listen(120)
print("success")
while True:
client_socket, client_address = server_socket.accept()
print("[%s, %s]用户连接上了" % client_address)
handle_client_process = Process(target=handle_client, args=(client_socket,))
handle_client_process.start()
client_socket.close()
另外一种http方式
#-.- coding:utf-8 -.-
from http.server import HTTPServer
import ctypes
from ctypes import *
# HTTPRequestHandler class
import http.server
import socketserver
import logging
# pyinstaller -F
class testHTTPServer_RequestHandler(http.server.BaseHTTPRequestHandler):
# GET
def do_GET(self):
logging.error('start make ')
str2 = str(self.path)
print("revice: " + str2)
if "xxx" in str2:
# todo 你的具体业务操作
if "xxx" in str2:
print("hahaha")
logging.error('hahaha')
# response_body = "0"
self.send_response(200)
# Send headers
self.send_header('Content-type','text/html')
self.end_headers()
# Send message back to client
message = "Hello world!"
# Write content as utf-8 data
self.wfile.write(bytes(message, "utf8"))
return
else:
print("1else")
self.send_response(200)
# Send headers
self.send_header('Content-type', 'text/html')
self.end_headers()
# Send message back to client
message = "Hello world222333!"
# Write content as utf-8 data
self.wfile.write(bytes(message, "utf8"))
return
def run():
print('starting server...')
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
filename="http_make_card.txt",
filemode='a+'
)
# Server settings
server_address = ('127.0.0.1', 8888)
httpd = HTTPServer(server_address, testHTTPServer_RequestHandler)
print('running server...')
httpd.serve_forever()
run()
pip install pyinstaller
pyinstaller -F xxx.py 即可,当前目录下生成
1、No module named ‘http.server'; ‘http' is not a package
当时自己建了一个py叫http,删掉后正常
2、UnicodeDecodeError: ‘utf-8' codec can't decode byte 0xce in position 130: invalid continuat
另存为utf-8即可

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
python实现函数重载有什么方法?在python中,我们想要实现函数重载,可以利用装饰器。接下来就给大家分享一下python实现函数重载的过程及操作,感兴趣的朋友可以参考,对于python实现函数重载,下文有很详细的介绍。
游戏界面中文字也是非常常见的元素之一,pygame专门提供了Font模块来支持文字的显示,下面这篇文章主要给大家介绍了关于pygame学习笔记之设置字体及显示中文的相关资料,需要的朋友可以参考下
一、安装此处以Ubuntu12.04为例:$sudoapt-getinstallipythonpython-matplotlibpython-numpy二、简单实例>>>importmatplotlib.pyplotasplt>>>>>>plt.figure(1)
没到一年一度的520等节假日,作为一个地地道道的程序猿心里慌得一批,除了吃饭买礼物看电影好像就没有更多的想法了。本文教你用Python制作一个心型照片墙,需要的可以参考一下
这篇文章主要为大家介绍了Keras搭建分类网络平台VGG16 MobileNet ResNet50,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008