2013-07-06 36 views
0

我想創建一個服務器/客戶端在Python中使用套接字發送文本和其他媒體文件。 場景: -客戶端將主機,端口和文件名作爲參數並將文件發送到服務器。 錯誤描述: -嘗試執行下面的客戶端代碼,在與client.Getting下面的錯誤相同的目錄中有文本文件「tos」。從客戶端使用python套接字發送txt文件到服務器

**$ python Cli.py 127.0.0.1 5007 tos** 
Traceback (most recent call last): 
    File "Cli.py", line 32, in <module> 
    client= Client(host,port,file) 
    File "Cli.py", line 15, in __init__ 
    self.connect(file) 
    File "Cli.py", line 20, in connect 
    self.sendFile(file) 
    File "Cli.py", line 26, in sendFile 
    readByte = open(file, "rb") 
**IOError: [Errno 2] No such file or directory: ''** 

注意: - 還請描述是否有任何文件發送到服務器,搜索硬盤驅動器。

服務器: -

from socket import * 
port = 5007 
file = '' 
class Server: 
    gate = socket(AF_INET, SOCK_STREAM) 
    host = '127.0.0.1' 
    def __init__(self, port): 
     self.port = port 
     self.gate.bind((self.host, self.port)) 
     self.listen() 

    def listen(self): 
     self.gate.listen(10) 
     while True: 
      print("Listening for connections, on PORT: ", self.port) 
      add = self.gate.accept() 
      self.reciveFileName() 
      self.reciveFile() 


    def reciveFileName(self): 
     while True: 
      data = self.gate.recv(1024) 
      self.file = data 

    def reciveFile(self): 
     createFile = open("new_"+self.file, "wb") 
     while True: 
      data = self.gate.recv(1024) 
      createFile.write(data) 
     createFile.close() 
server= Server(port) 
listen() 

客戶: -

#!/usr/bin/env python 
from socket import * 
host = '' 
port = 5007 
file = '' 
class Client: 
    gateway = socket(AF_INET, SOCK_STREAM) 
    def __init__(self, host,port, file): 
     self.port = port 
     self.host = host 
     self.file = file 
     self.connect() 

    def connect(self): 
     self.gateway.connect((self.host, self.port)) 
     self.sendFileName(file) 
     self.sendFile(file) 

    def sendFileName(self): 
     self.gateway.send("name:" +self.file) 

    def sendFile(self): 
     readByte = open(self.file, "rb") 
     data = readByte.read() 
     readByte.close() 

     self.gateway.send(data) 
     self.gateway.close() 
client= Client(host,port,file) 
connect() 
+0

你有沒有打算給這個文件命名? –

+0

該場景是我不想硬編碼的文件名。而是希望客戶端程序通過命令行發送文件併發送它。 – Noddy

+0

是的...但你沒有這樣做... –

回答

0

目前file = ''這不是一個有效的文件名。爲了清楚起見,我還建議將file更名爲filename

+0

您好Steve,修改文件作爲文件名。同樣的錯誤。 回溯(最近通話最後一個): 文件 「Cli.py」,第29行,在 客戶端=客戶端(主機,端口,文件名) 文件 「Cli.py」,第12行,在__init__ self.connect () 文件「Cli.py」,第17行,連接 self.sendFile() sendFile中的第23行文件「Cli.py」 readByte = open(self.filename,「rb」) IOError: [Errno 2]沒有這樣的文件或目錄:'' – Noddy

+0

但是你仍然沒有證明任何內容的文件名 - 即self.filename ='./Fred.txt'應導致從當前目錄Fred.txt發送或和錯誤消息說「[Errno 2]沒有這樣的文件或目錄:'./Fred.txt'」 –

+0

其他選項wou ld是提供文件名作爲命令行參數或提示用戶 - 查看sys.argv和rawinput的文檔作爲起點。 –

0

將此任務作爲家庭作業3個月前。 對此的解決方案非常簡單 - 只需讀取文件,將相關文本放在字符串變量中併發送。看看這個服務器代碼:

HOST = '192.168.1.100' 
PORT = 8012 
BUFSIZE = 1024 
ADDR = (HOST, PORT) 
serversock = socket(AF_INET, SOCK_STREAM) 
serversock.bind(ADDR) 
serversock.listen(SOMAXCONN) 

fileOpen = open("D:/fileLocation.txt") 
g = f.read() 
print 'Waiting For Connection..' 
clientsock, addr = serversock.accept() 
print 'Connection Established From: ', addr` 
clientsock.sendall(g) 

這是這樣做的一個非常簡單的方式

客戶端只需接收數據(作爲文本)並將其保存在想要的位置。

爲我工作的BMP,PNG和JPG圖像也。

+0

嗨C0LDSH3LL, 感謝您的答覆。但事實上,我的查詢有點差異。我需要我的客戶端程序在我的硬盤上查找文件,找到它並將其發送到服務器。 作爲輸入我必須給客戶端程序的主機端口和文件名。 – Noddy

+0

好吧,那麼只需詢問文件名,然後在C:/或D:/中使用簡單的for循環來搜索它。 基本上這將是一個奇怪的事情要做,因爲你可以要求用戶只需選擇文件的位置 – c0ldsh3ll

相關問題