I have a server:
import socket
import time
import random
from PIL import ImageGrab
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1',8820))
server_socket.listen(1)
while True:
print "Waiting for commands"
a = ImageGrab.grab()
CommandDict = {"TIME" : time.strftime("%a %b %d %H:%M:%S %Y"),"NAME": "Ori","RANDOM": str(random.randint(0,10)),"EXIT":"BYE","PRINT SCREEN":a}
(client_socket, client_address) = server_socket.accept()
client_data = client_socket.recv(1024)
print "GOT COMMAND FROM " + client_address[0] + " : " + client_data
client_socket.send(CommandDict[client_data])
client_socket.close()
server_socket.close()
and the client:
import socket
from PIL import ImageGrab
while True:
client_input = raw_input("Enter You Command: ")
CommandList=["EXIT","TIME","NAME","RANDOM","PRINT SCREEN"]
if client_input in CommandList:
my_socket=socket.socket()
my_socket.connect(('127.0.0.1',8820))
my_socket.send(client_input)
data = my_socket.recv(1024) + "\n"
if client_input=="PRINT SCREEN":
data.save()
else:
print "SERVER: " + data
else:
print "Invalid command. try one of those: " + " , ".join(CommandList) + "\n" + "."
my_socket.close()
When i try this it gives me an error because it trys to send it as a string object. I want to send an object through socket and i want the client to read it. Any ideas?