起因
服务器为内部服务器,从本机没有直连渠道,只能基于novnc web管理,与本地之间无法互传文件。
思路
从本地启动临时http服务,可以通过命令行上传,下载文件,可以限制非服务器IP无法连接。
本地临时http代码
1、share.py (将服务器公网出口IP填入ALLOWED_IPS )
import http.server
import socketserver
import os
from http import HTTPStatus
import cgi
# 指定允许的IP地址
ALLOWED_IPS = ['192.168.1.2', '192.168.2.66'] # 请替换为实际的IP地址
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# 检查客户端IP是否在允许列表中
if self.client_address[0] in ALLOWED_IPS:
super().do_GET()
else:
self.send_error(HTTPStatus.FORBIDDEN, "Access denied")
def do_POST(self):
# 检查客户端IP
if self.client_address[0] in ALLOWED_IPS:
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
filename = form['file'].filename
file_data = form['file'].file.read()
with open(filename, 'wb') as f:
f.write(file_data)
self.send_response(HTTPStatus.OK)
self.end_headers()
self.wfile.write(b"File uploaded successfully")
else:
self.send_error(HTTPStatus.FORBIDDEN, "Access denied")
def run(server_class=http.server.HTTPServer, handler_class=CustomHTTPRequestHandler):
server_address = ('', 1234)
httpd = server_class(server_address, handler_class)
print("Starting httpd server on port 1234")
httpd.serve_forever()
if __name__ == "__main__":
run()
2、share.bat
@echo off
setlocal
cd %USERPROFILE%\Desktop
if not exist share (
mkdir share
)
cd %USERPROFILE%\Desktop\share
cmd /k "python share.py"
endlocal
服务器临时下载上传文件
1、下载文件命令
wget http://xxxx.com:1234/a.zip
2、上传文件命令,其中/path/to/file为要上传的文件路径,可以是相对路径,注意:服务器上已存在同名文件会覆盖。
curl -X POST -F "file=@/path/to/file" http://xxxx.com:1234
标签:http,命令行,临时,self,share,server,传输数据,file,import
From: https://www.cnblogs.com/dwj192/p/17901203.html