简体   繁体   English

无法使用Python套接字编程在计算机之间发送文件

[英]Unable to send files between computers using Python socket programming

I wrote a socket programming code in Python. 我用Python编写了套接字编程代码。 It is supposed to be able to send files between different computers. 它应该能够在不同的计算机之间发送文件。 I tested my code on the same computer (ie. by sending files from 2 different folders) and it works. 我在同一台计算机上测试了我的代码(即通过从2个不同的文件夹发送文件),它可以工作。 However, when I try to test the code by sending the files from my computer to my friend's computer, I keep getting this error: 但是,当我尝试通过将文件从计算机发送到朋友的计算机来测试代码时,我一直收到此错误:

 WindowsError: [Error 3] The system cannot find the path specified: 'C://Users//Admin//Desktop//SharedFiles/*.*' 

The error occurs when my Python program tried to access the client_side_folder variable as shown below: 当我的Python程序尝试访问client_side_folder变量时发生错误,如下所示:

client_side_folder = "C://Users//Admin//Desktop//SharedFiles"

I have also tried replacing the client_side_folder as "C:/Users/Admin/Desktop/SharedFiles" and "C:\\Users\\Admin\\Desktop\\SharedFiles". 我还尝试将client_side_folder替换为“ C:/ Users / Admin / Desktop / SharedFiles”和“ C:\\ Users \\ Admin \\ Desktop \\ SharedFiles”。 But both still have the same error. 但是两者仍然有相同的错误。

Does this error got to do with how Python deal with Windows address? 这个错误与Python如何处理Windows地址有关吗? If so, how come it works when I tried to send files from 1 folder to another folder in the same computer? 如果是这样,当我尝试将文件从1个文件夹发送到同一台计算机上的另一个文件夹时,它如何工作? Any ideas how I can solve this problem? 有什么想法可以解决这个问题吗?

EDIT: 编辑:

The code crashes where it hit the following code: 代码在命中以下代码时崩溃:

        def listOfFiles(directory): 
                list_dir = os.listdir(directory) 
                return list_dir

You might find it easier to handle paths in python, through the os.path module. 您可能会发现通过os.path模块更容易处理python中的路径。 For instance, if you want to access a file in the home directory of a user: 例如,如果要访问用户主目录中的文件:

from os.path import expanduser, join

userhome = expanduser( '~' )
desktop = join( userhome, 'Desktop' )
shared_dir = join( desktop, 'SharedFiles' )
file_list = os.listdir( shared_dir )



This also implies that the user executing the script has access privileges to whatever path you're trying to access. 这也意味着执行脚本的用户对您尝试访问的任何路径都具有访问权限。

addendum 1 : 附录1

Assuming you're using tcp sockets this a sample code for client and server: 假设您正在使用tcp套接字,这是客户端和服务器的示例代码:

server running on your friend's host: 在您朋友的主机上运行的服务器:

import socket
serversocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
serversocket.bind( ( 'localhost', 13000 ) )
serversocket.listen(5) # 5 is the number of client connections that can be queued
while 1:
    #accept connections from outside
    (clientsocket, address) = serversocket.accept()
    # processing of the incoming connection
    ....

client running on your host: 在您的主机上运行的客户端:

import socket
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.connect( ( 'friendshost', 13000 ) )
s.send( .... )

The Problem 问题

The code that is breaking is: 破坏的代码是:

list_dir = os.listdir(directory) 

This means the directory pointed to by directory does not exist in your local machine. 这意味着目录指向的directory在您的本地计算机中不存在。

In your code, you have 在您的代码中,您有

client_files = listOfFiles(client_side)
server_files = listOfFiles(server_side)

which won't work. 这是行不通的。

You cannot use os.listdir to get the listing of a folder on a remote machine (friend's machine) without using something like \\\\hostname\\\\path . 如果不使用\\\\hostname\\\\path则无法使用os.listdir获取远程计算机(朋友的计算机)上的文件夹列表。

When you are running the server and client on same machine, it works as both points to the local machine. 当您在同一台计算机上运行服务器和客户端时,它既可以指向本地计算机,也可以用作本地计算机。

Solution

Method 1: Stop trying to get the listings of the remote machine and let the code running on that machine do it for you. 方法1:停止尝试获取远程计算机的清单,然后让在该计算机上运行的代码为您执行此操作。

Method 2: Use remote access URL (shared folder or something) to access the details of the remote machine. 方法2:使用远程访问URL(共享文件夹或其他内容)来访问远程计算机的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM