without saving to disk send to server by sokcet like part of JSON with JSON-structure data = ('user_id': user_id, 'filename': filename, 'file_source': file_source)
on server side take JSON and save screenshots with name filename and source of file file_source
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's the Python code for the client-side and server-side parts of your requirements:
Client-side code:
python
import mss
import json
import socket
import uuid
def take_screenshots():
with mss.mss() as sct:
screenshots = []
for monitor in sct.monitors:
screenshot = sct.grab(monitor)
screenshot_bytes = mss.tools.to_png(screenshot.rgb, screenshot.size)
screenshots.append(screenshot_bytes)
return screenshots
def send_screenshots_to_server(user_id, screenshots):
host = 'localhost' # Replace with the server's IP address
port = 12345 # Replace with the server's port number
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((host, port))
for screenshot in screenshots:
filename = str(uuid.uuid4()) + '.png'
data = {
'user_id': user_id,
'filename': filename,
'file_source': screenshot
}
json_data = json.dumps(data)
sock.sendall(json_data.encode())
def main():
user_id = 'user123' # Replace with the actual user ID
screenshots = take_screenshots()
send_screenshots_to_server(user_id, screenshots)
if __name__ == '__main__':
main()
Server-side code:
python
import socket
import json
import os
def receive_screenshots():
host = 'localhost' # Replace with the server's IP address
port = 12345 # Replace with the server's port number
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind((host, port))
sock.listen(1)
print('Server is listening on {}:{}'.format(host, port))
while True:
conn, addr = sock.accept()
print('Connected by', addr)
data = b''
while True:
chunk = conn.recv(4096)
if not chunk:
break
data += chunk
json_data = json.loads(data.decode())
user_id = json_data['user_id']
filename = json_data['filename']
file_source = json_data['file_source']
# Create a directory for the user if it doesn't exist
user_dir = os.path.join('screenshots', user_id)
os.makedirs(user_dir, exist_ok=True)
# Save the screenshot to the user's directory
file_path = os.path.join(user_dir, filename)
with open(file_path, 'wb') as f:
f.write(file_source)
print('Screenshot saved:', file_path)
if __name__ == '__main__':
receive_screenshots()
In the client-side code:
The take_screenshots() function uses the mss library to capture screenshots from all monitors.
The send_screenshots_to_server() function establishes a socket connection with the server and sends the screenshots as part of a JSON structure.
The main() function orchestrates the process by capturing the screenshots and sending them to the server.
In the server-side code:
The receive_screenshots() function sets up a socket server and listens for incoming connections.
When a connection is established, it receives the JSON data from the client.
It extracts the user_id, filename, and file_source from the JSON data.
It creates a directory for the user if it doesn't exist and saves the screenshot with the specified filename in the user's directory.
Note: Make sure to replace 'localhost' with the actual IP address of the server and choose an appropriate port number. Also, ensure that the necessary libraries (mss, json, socket, uuid, os) are installed on both the client and server machines.