lựa chọn 1
Như đã đề cập trước đây ở đây, cũng như ở đây và ở đây, người ta nên sử dụng
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
4, để tải lên các tệp từ các ứng dụng khách [để đọc/ghi import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
5 hãy xem câu trả lời này]. Ví dụ:Phía máy chủ:
@app.post["/upload"]
def upload[file: UploadFile = File[...]]:
try:
contents = file.file.read[]
with open[file.filename, 'wb'] as f:
f.write[contents]
except Exception:
return {"message": "There was an error uploading the file"}
finally:
file.file.close[]
return {"message": f"Successfuly uploaded {file.filename}"}
phía khách hàng:
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
Lựa chọn 2
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thành import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0 và gửi nó bằng yêu cầu import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
2 như sau:phía khách hàng:
import base64
import requests
url = '//127.0.0.1:8000/upload'
with open["photo.png", "rb"] as image_file:
encoded_string = base64.b64encode[image_file.read[]]
payload ={"filename": "photo.png", "filedata": encoded_string}
resp = requests.post[url=url, data=payload]
Lựa chọn 2
Phía máy chủ:
@app.post["/upload"]
def upload[filename: str = Form[...], filedata: str = Form[...]]:
image_as_bytes = str.encode[filedata] # convert string to bytes
img_recovered = base64.b64decode[image_as_bytes] # decode base64string
try:
with open["uploaded_" + filename, "wb"] as f:
f.write[img_recovered]
except Exception:
return {"message": "There was an error uploading the file"}
return {"message": f"Successfuly uploaded {filename}"}
lựa chọn 1
phía khách hàng:
Phía máy chủ:
@app.post["/upload"]
async def upload[file: UploadFile = File[...]]:
try:
contents = await file.read[]
with open[file.filename, 'wb'] as f:
f.write[contents]
except Exception:
return {"message": "There was an error uploading the file"}
finally:
await file.close[]
return {"message": f"Successfuly uploaded {file.filename}"}
phía khách hàng:
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
Lựa chọn 2
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thành import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
0 và gửi nó bằng yêu cầu import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
2 như sau:phía khách hàng:
import base64
import requests
url = '//127.0.0.1:8000/upload'
with open["photo.png", "rb"] as image_file:
encoded_string = base64.b64encode[image_file.read[]]
payload ={"filename": "photo.png", "filedata": encoded_string}
resp = requests.post[url=url, data=payload]
Lựa chọn 2
Phía máy chủ:
import requests
url = '//127.0.0.1:8000/upload'
file = {'file': open['images/1.png', 'rb']}
resp = requests.post[url=url, files=file]
print[resp.json[]]
3phía khách hàng:
Lựa chọn 2
importcv2cv2 cv2 | |
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa socketio #python-socketio by @miguelgrinberg | |
importbase64base64 base64 | |
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thành 0 và gửi nó bằng yêu cầu 2 như sau: = socketio.Client[]
| |
Như đã đề cập trước đây ở đây, ở đây và ở đây, người ta nên sử dụng 4, để tải lên các tệp từ các ứng dụng khách. Ví dụ:.connect['http://x.x.x.x:xxxx]
| |
socket.on ['data', function [data] {// nghe trên máy khách phát ra 'dữ liệu' = cv2.VideoCapture[0] | |
Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây.Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn.Tìm hiểu thêm về các ký tự unicode hai chiều [True]: | |
Nhập khẩu#Python-Socketio của @MiguelGrinbergsocketio#python-socketio by @miguelgrinberg, frame = cam.read[] # get frame from webcam | |
Sio = socketio.client []=socketio.Client[] , frame = cv2.imencode['.jpg', frame] # from image to binary buffer | |
Sio.connect [' //x.x.x.x: xxxx].connect['//x.x.x.x:xxxx] = base64.b64encode[frame] # convert to base64 format | |
cam = cv2.videocapture [0]=cv2.VideoCapture[0] .emit['data', data] # send to server | |
while [đúng]: [True]: .release[] | |
Lựa chọn 2
varapp=require['express'][];app=require['express'][]; app = require['express'][]; | |
varhttp=require['http'].createServer[app];http=require['http'].createServer[app]; http = require['http'].createServer[app]; | |
vario=require['socket.io'][http];io=require['socket.io'][http]; io = require['socket.io'][http]; | |
varsizeof=require['object-sizeof'];sizeof=require['object-sizeof']; sizeof = require['object-sizeof']; | |
app.get['/',function[req,res]{.get['/',function[req,res]{.get['/', function [req, res] { | |
res.send['running'];.send['running'];.send['running']; | |
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa ] | |
io.on['connection',function[socket]{.on['connection',function[socket]{.on['connection', function [socket] { | |
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thành 0 và gửi nó bằng yêu cầu 2 như sau:.on['data', function [data] { // listen on client emit 'data'
| |
varret=Object.assign[{},data,{ret=Object.assign[{},data,{ ret = Object.assign[{}, data, { | |
Như đã đề cập trước đây ở đây, ở đây và ở đây, người ta nên sử dụng 4, để tải lên các tệp từ các ứng dụng khách. Ví dụ:: Buffer.from[data.frame, 'base64'].toString[] // from buffer to base64 string
| |
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa ] | |
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thành 0 và gửi nó bằng yêu cầu 2 như sau:.emit['data', ret]; // emmit to socket
| |
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa ] | |
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa ] | |
http.listen[3000,function[]{.listen[3000,function[]{.listen[3000, function [] { | |
0, bạn có thể làm điều đó như được mô tả trước đây ở đây [tùy chọn 2]. Về phía máy khách, bạn có thể mã hóa hình ảnh thành 0 và gửi nó bằng yêu cầu 2 như sau:.log['listening on *:3333'];
| |
Tuy nhiên, nếu bạn vẫn cần gửi hình ảnh được mã hóa ] |