Python 如何模拟发送 HTTP MOVE 方法的报文呢?

null
Python 如何模拟发送 HTTP MOVE 方法的报文呢?

6 回复

shell=‘MOVE /fileserver/2.txt HTTP/1.1\r\n’
shell+=‘Host: 192.168.132.130:8161\r\n’
shell+=‘Destination: file:///’+self.get_install_path()+’/webapps/api/s.jsp’+’\r\n’
shell+=‘Content-Lenth:0\r\n’
test_HOST=‘192.168.132.130’
test_PORT=8161
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((test_HOST,test_PORT))
s.send(shell)
except Exception,e:
pass
finally:
s.close()


requests 库的话,得用 requests.request('MOVE', url, ...),因为它没封装成直接的方法。或者用 httpx 库,它支持 .request('MOVE', ...),用法差不多。

下面是个用 requests 的完整例子:

import requests

url = 'http://example.com/resource'
headers = {
    'Destination': 'http://example.com/new_location',  # MOVE 方法通常需要这个头指定目标位置
    'Overwrite': 'T'  # 可选,处理覆盖行为
}

response = requests.request('MOVE', url, headers=headers)

print(f'状态码: {response.status_code}')
print(f'响应头: {response.headers}')
print(f'响应体: {response.text}')

关键点:

  1. requests.request() 第一个参数传方法名 'MOVE'
  2. MOVE 方法(类似 WebDAV 操作)通常需要 Destination 请求头来指定移动的目标 URI。
  3. 服务器可能还需要认证,可以在 requests.request() 里加 auth=... 参数。

如果服务器支持且配置正确,这就能模拟发送 MOVE 请求了。

总结:用 requests.request()httpx 直接指定方法名。

那个的 MOVE 方法?WebDAV?

嗯,对

谢谢,之前看文档错过了这段,我也发现我自己写的错在哪里了,那个 HTTP 协议规定尾部是两个回车,所以应该是\r\n\r\n

回到顶部