python运行程序提示错误:HTTP Error 503. The service is unavailable.

本文介绍了解决IIS(Internet Information Services)中网站无法正常浏览的问题步骤。具体包括:通过控制面板启用IIS功能,修改应用程序池的内置账户为NetworkService,并重启应用池等操作。

解决办法:

1.打开控制面板
2.进入控制面板之后点击程序
3.找到启用或关闭Windows功能
4.在“Windows功能”对话框里选中“Internet Information Services”,并点击“确定”按钮
5.配置完成之后,点击确定,慢慢等待,可以看到正在应用所做的更改 的进度条,完成之后会提示已完成请求的更改,关闭即可;
6.在搜索栏中输入IIS管理器,找到IIS管理器并打开
7.在应用程序池上–右键–高级设置–进程模型–标识,更改了这项里的“内置账户”。将原有的“ApplicationPoolIdentity”更改为“NetworkService”。然后重启下应用池,所有网站浏览一切都OK了。
在这里插入图片描述
在这里插入图片描述

参考:
http://www.xitongcheng.com/jiaocheng/win10_article_30867.html
https://blog.youkuaiyun.com/veloi/article/details/83509203

/Users/10280281/PycharmProjects/PythonProject/.venv/bin/python /Users/10280281/PycharmProjects/PythonProject/.venv/lib/python3.9/python_course/module2_functions/lesson_requests.py /Users/10280281/PycharmProjects/PythonProject/.venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 warnings.warn( 🚀 开始演示 requests 核心请求方法详解 === 示例 1:最基础的 GET 请求 === 状态码: 200 响应内容(前50字符): { "userId": 1, "id": 1, "title": "sunt aut f... === 示例 2:带查询参数的 GET 请求 === 完整URL: https://jsonplaceholder.typicode.com/posts?userId=1&_limit=2 返回数据: ID: 1, 标题: sunt aut facere repellat provident occaecati excepturi optio reprehenderit ID: 2, 标题: qui est esse === 示例 3:带请求头的 GET 请求 === 响应头 Content-Type: application/json; charset=utf-8 第一条数据标题: sunt aut facere repellat provident occaecati excepturi optio reprehenderit === 示例 4:带超时和异常处理的 GET 请求 === 请求成功,返回数据条数: 100 === 示例 5:最基础的 POST 请求(JSON 数据) === 状态码: 201 返回数据 ID: 101 === 示例 6:POST 发送表单数据 === 服务器收到的数据: 响应内容不是有效的 JSON!原始内容: <html> <head><title>503 Service Temporarily Unavailable</title></head> <body> <center><h1>503 Service Temporarily Unavailable</h1></center> </body> </html> === 示例 7:POST 发送 JSON + 自定义 Headers === 返回标题: 高级文章 === 示例 8:POST 发送 JSON + 超时和异常处理 === 创建成功,ID: 101 === 示例 9:使用 PUT 请求更新资源 === 状态码: 200 更新后的标题: 更新后的标题 === 示例 10:使用 DELETE 请求删除资源 === 状态码: 200 响应内容: {} === 示例 11:最佳实践 - 完整请求流程 === 文章创建成功,ID: 101 ✅ 所有演示完成 进程已结束,退出代码为 0 你这里发送表单数据返回的不对呀,需要找一个开源可用的传表单数据的接口:# -*- coding: utf-8 -*- """ ============================ requests 课件:HTTP 请求核心方法详解 ============================ 版本:3.1 | 修复 JSON 解码错误 + 最佳实践示例 📌 使用的免费测试接口: 1. https://jsonplaceholder.typicode.com/ - 提供标准 REST API 接口 2. https://httpbin.org/ - 提供请求测试和调试接口 """ # 🧠 What:requests 是什么? # requests 是 Python 中最常用的 HTTP 客户端库,用于向 Web 服务器发送各种类型的请求。 # 它广泛应用于 API 调试、接口测试、网络爬虫等场景。 # 🛠 How:怎么使用? # - 使用 requests.get() 发送 GET 请求 # - 使用 requests.post() 发送 POST 请求 # - 使用 requests.put() 发送 PUT 请求 # - 使用 requests.delete() 发送 DELETE 请求 # - 使用 params、data、json 控制请求体 # - 使用 headers 设置请求头 # - 使用 timeout 控制超时时间 # - 使用异常处理保障程序健壮性 # 🎯 Why:为什么要用 requests? # - 简洁易用,语法直观 # - 支持多种 HTTP 方法 # - 自动处理编码和解码 # - 社区活跃,文档丰富 # - 适用于自动化脚本、接口测试、爬虫等 import requests # ==================== # 第一部分:GET 请求详解 # ==================== # 示例 1:最基础的 GET 请求 def demo_get_simple(): """最基础的 GET 请求示例""" print("=== 示例 1:最基础的 GET 请求 ===") url = "https://jsonplaceholder.typicode.com/posts/1" response = requests.get(url) print(f"状态码: {response.status_code}") print("响应内容(前50字符):") print(response.text[:50] + "...") # 示例 2:带查询参数的 GET 请求 def demo_get_with_params(): """带查询参数的 GET 请求示例""" print("=== 示例 2:带查询参数的 GET 请求 ===") url = "https://jsonplaceholder.typicode.com/posts" params = { "userId": 1, "_limit": 2 } response = requests.get(url, params=params) print(f"完整URL: {response.url}") print("返回数据:") for item in response.json(): print(f"ID: {item['id']}, 标题: {item['title']}") # 示例 3:带请求头的 GET 请求 def demo_get_with_headers(): """带请求头的 GET 请求示例""" print("=== 示例 3:带请求头的 GET 请求 ===") url = "https://jsonplaceholder.typicode.com/posts" headers = { "User-Agent": "MyApp/1.0", "Accept": "application/json" } response = requests.get(url, headers=headers) print(f"响应头 Content-Type: {response.headers['Content-Type']}") print("第一条数据标题:", response.json()[0]['title']) # 示例 4:带超时和异常处理的 GET 请求 def demo_get_with_timeout(): """带超时和异常处理的 GET 请求示例""" print("=== 示例 4:带超时和异常处理的 GET 请求 ===") url = "https://jsonplaceholder.typicode.com/posts" try: response = requests.get(url, timeout=3) response.raise_for_status() print("请求成功,返回数据条数:", len(response.json())) except requests.exceptions.Timeout: print("请求超时!") except requests.exceptions.HTTPError as e: print(f"HTTP 错误: {e}") except requests.exceptions.RequestException as e: print(f"请求错误: {e}") # ==================== # 第二部分:POST 请求详解 # ==================== # 示例 5:最基础的 POST 请求(JSON 数据) def demo_post_simple_json(): """最基础的 POST 请求(JSON 数据)""" print("=== 示例 5:最基础的 POST 请求(JSON 数据) ===") url = "https://jsonplaceholder.typicode.com/posts" data = { "title": "新文章", "body": "这是内容", "userId": 1 } response = requests.post(url, json=data) print(f"状态码: {response.status_code}") print("返回数据 ID:", response.json()['id']) # 示例 6:POST 发送表单数据(修复 JSON 解码错误) def demo_post_form_data(): """POST 发送表单数据示例""" print("=== 示例 6:POST 发送表单数据 ===") url = "https://httpbin.org/post" data = { "username": "testuser", "password": "testpass" } response = requests.post(url, data=data) print("服务器收到的数据:") try: # 尝试解析 JSON result = response.json() print(result.get('form', '未找到 form 数据')) except requests.exceptions.JSONDecodeError: print("响应内容不是有效的 JSON!原始内容:") print(response.text) # 示例 7:POST 发送 JSON + 自定义 Headers def demo_post_json_with_headers(): """POST 发送 JSON + 自定义 Headers 示例""" print("=== 示例 7:POST 发送 JSON + 自定义 Headers ===") url = "https://jsonplaceholder.typicode.com/posts" data = { "title": "高级文章", "body": "带自定义头", "userId": 1 } headers = { "Content-Type": "application/json", "X-Requested-With": "XMLHttpRequest" } response = requests.post(url, json=data, headers=headers) print("返回标题:", response.json()['title']) # 示例 8:POST 发送 JSON + 超时和异常处理 def demo_post_with_timeout(): """POST 发送 JSON + 超时和异常处理示例""" print("=== 示例 8:POST 发送 JSON + 超时和异常处理 ===") url = "https://jsonplaceholder.typicode.com/posts" data = { "title": "测试超时", "body": "超时测试内容", "userId": 1 } try: response = requests.post(url, json=data, timeout=3) response.raise_for_status() print("创建成功,ID:", response.json()['id']) except requests.exceptions.Timeout: print("请求超时!") except requests.exceptions.RequestException as e: print(f"请求失败: {e}") # ==================== # 第三部分:PUT 请求详解 # ==================== # 示例 9:使用 PUT 更新资源 def demo_put_update(): """使用 PUT 请求更新资源示例""" print("=== 示例 9:使用 PUT 请求更新资源 ===") url = "https://jsonplaceholder.typicode.com/posts/1" data = { "id": 1, "title": "更新后的标题", "body": "更新后的内容", "userId": 1 } response = requests.put(url, json=data) print(f"状态码: {response.status_code}") print("更新后的标题:", response.json()['title']) # ==================== # 第四部分:DELETE 请求详解 # ==================== # 示例 10:使用 DELETE 删除资源 def demo_delete_resource(): """使用 DELETE 请求删除资源示例""" print("=== 示例 10:使用 DELETE 请求删除资源 ===") url = "https://jsonplaceholder.typicode.com/posts/1" response = requests.delete(url) print(f"状态码: {response.status_code}") print("响应内容:", response.text) # ==================== # 第五部分:综合实战(最佳实践示例) # ==================== # 示例 11:最佳实践 - 完整请求流程(headers + timeout + session + 异常处理) def demo_best_practice(): """最佳实践示例:完整请求流程 + 安全调用""" print("=== 示例 11:最佳实践 - 完整请求流程 ===") url = "https://jsonplaceholder.typicode.com/posts" headers = { "User-Agent": "BestPracticeApp/1.0", "Accept": "application/json" } data = { "title": "最佳实践文章", "body": "这是一篇测试文章", "userId": 1 } with requests.Session() as session: session.headers.update(headers) try: response = session.post(url, json=data, timeout=5) response.raise_for_status() print("文章创建成功,ID:", response.json()['id']) except requests.exceptions.Timeout: print("请求超时,请检查网络") except requests.exceptions.HTTPError as e: print(f"HTTP 错误: {e}") except requests.exceptions.RequestException as e: print(f"请求异常: {e}") # ==================== # 主函数:运行所有示例 # ==================== if __name__ == "__main__": print("🚀 开始演示 requests 核心请求方法详解") demo_get_simple() demo_get_with_params() demo_get_with_headers() demo_get_with_timeout() demo_post_simple_json() demo_post_form_data() demo_post_json_with_headers() demo_post_with_timeout() demo_put_update() demo_delete_resource() demo_best_practice() # 最佳实践 print("\n✅ 所有演示完成")
最新发布
09-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值