我在 python 中有一个服务器端程序,它需要一个图像,并且在使用 python 中的客户端程序进行测试时工作正常。
我想使用颤振将图像发送到该服务器,但我没有这样做..
这是我的服务器端代码
import socket #server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET = IP, SOCK_STREAM = TCP
server.bind(('localhost', 1112)) # 127.0.0.1
server.listen()
client_socket, client_address = server.accept()
file = open('2.jpg', "wb")
image_chunk = client_socket.recv(1024) # stream-based protocol
while image_chunk:
file.write(image_chunk)
image_chunk = client_socket.recv(1024)
file.close()
client_socket.close()
我试过使用dio
,http
和MultiPart
以下是我失败尝试的片段:
MultiPart
var uri = Uri.parse('https://10.0.2.2:1112'); var request = MultipartRequest('POST', uri) ..files.add(await MultipartFile.fromPath( 'picture', filePath, contentType: MediaType('application', 'jpeg'))); var response = await request.send(); if (response.statusCode == 200) print('Uploaded!');
Dio
Dio dio = new Dio(); FormData formData = new FormData.fromMap({ "file": await MultipartFile.fromPath(filePath, filename: basename(filePath), contentType: MediaType('application', 'jpeg'),) }); await dio.post('https://10.0.2.2:1112', data: formData);
我可以创建连接,但无法发送文件。
PS:我几乎没有使用套接字的经验,所以我陷入了困境。