Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何将变量从服务器传输到客户端。python_Python 2.7_Variables_Client Server_Transfer - Fatal编程技术网

Python 2.7 如何将变量从服务器传输到客户端。python

Python 2.7 如何将变量从服务器传输到客户端。python,python-2.7,variables,client-server,transfer,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 impo

因此,我对使用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()
正如你所看到的,这是非常简单的事情,但我一直坚持的是如何在Show files服务器部分传输变量,服务器向客户端发送列出的文件列表。它臃肿不堪。
如果你能帮我,那就太好了,我已经坚持了好几个星期了。我后来解决了这个问题。 您需要做的就是更好地使用struct库:

在服务器上:

文件列表='\n'.joinfile\u列表 文件\u列表\u len=lenfiles\u列表 客户端\u name.sendfiles\u列表\u len 客户端\u name.sendfile\u列表 客户端:

recv_size=client_socket.recv4 files=client\u socket.recvrecv\u size.split\n