如何将变量从服务器传输到客户端。蟒蛇

时间:2014-10-31 10:22:06

标签: python-2.7 variables client-server transfer

所以我对使用python的服务器和客户端代码真的很陌生,我正在从这本书中做这个项目,要求我做一些事情。其中之一是传输所有文件,以便客户端可以清楚地看到它们。这是代码。

服务器:

import socket
from PIL import ImageGrab
import sys 
sys.path.append(r'C:\Users\Home\Documents\python\new_project')
import pyperclip
import shutil
import glob
import subprocess

host = socket.gethostname()
port = 12346
server_socket = socket.socket()
server_socket.bind((host, port))
Server_online = True

while Server_online:
server_socket.listen(2)

client_socket, client_addr = server_socket.accept()
client_name = client_socket.recv(1024)

if client_name == "Screen Shot":
    im = ImageGrab.grab()
    f = open('C:\Users\Home\Documents\screen.txt')
    i = int(f.read())
    f.close()
    im.save(r"C:\Users\Home\Documents\python\new_project\screen" + str(i) +'.jpg' )
    i = i + 1
    f = open('C:\Users\Home\Documents\screen.txt', 'w')
    f.write(str(i))
    f.close()
    client_socket.send("Picture taken")

elif client_name == "Copy":
    while 1:
        try:
            data = client_socket.recv(1024)
            Copied = pyperclip.copy(data)
            client_socket.send("text copied.")
            break
        except:
            client_socket.send("Failed to copy")

elif client_name == "Paste":
    pasting = pyperclip.paste()

elif client_name == "Show files":
    while 1:
        files = client_socket.recv(1024)
        files_list = glob.glob("C\\Users\\Home\\Documents\\" + files + "\\*.*")
        client_socket.send(**files_list**)
        break
elif client_name == "Exit":
    client_socket.sendall("Exting server...")
    client_socket.close()







 client_socket.close()
 server_socket.close()

客户端:

 import socket
 import time
 import glob
 host = socket.gethostname()
 port = 12346

 client_socket = socket.socket()
 client_socket.connect((host, port))
 client_connected = True
 while client_connected:
        print '''Pick What you want to do.(Screen Shot, Copy, Paste, 
Show files, Open files, Copy files, Exit )'''
request = raw_input()
if request == "Screen Shot":
    print "Screen shot in 5 seconds"
    for i in range(5,0,-1):
        print i
        time.sleep(1)
    client_socket.send("Screen Shot")
    print client_socket.recv(1024)

elif request == "Copy":
    client_socket.send("Copy")
    print "what do u want to be copied:"
    Copied = raw_input()
    client_socket.send(Copied)
    print client_socket.recv(1024)

elif request == "Exit":
    client_socket.send("Exit")
    print client_socket.recv(1024)
    break

elif request == "Paste":
    client_socket.send("Paste")

elif request == "Show files":
    client_socket.send("Show files")
    print "What files do you want to send?"
    files = raw_input()
    client_socket.send(files)
    outcome = client_socket.recv(1024)
    try:
        for files in outcome:
            print files
    except:
        print "no outcome"





else:
    print "Not one of the requests. Enter again."







client_socket.close()

因为你可以看到它真的很容易,但我坚持的是如何在“显示文件”(服务器)部分中传输变量,其中服务器向客户端发送列出的列出的文件。 (它的臃肿)。 如果你可以帮助我,那将是很好的,我已经坚持了几个星期

1 个答案:

答案 0 :(得分:0)

这太老了。后来我解决了这个问题。 您需要做的就是这个(最好用struct库来做):

在服务器上:

file_list = '\n'.join(file_list)
files_list_len = len(files_list)
client_name.send(files_list_len)
client_name.send(file_list)

客户端:

recv_size = client_socket.recv(4)
files = client_socket.recv(recv_size).split("\n")