Python Socket Programming: Upload files from Client to Server
I'm looking to find some assistance in my python socket programming.
Objective: upload a file from the Client to the Server. My current setup involves two virtual machines, a Server and a Client, where the respective .py files will reside; server.py and client.py . The issues I'm experiencing is, that when I go to choose the upload option, the output reads "File uploading.... File successfully uploaded" But when I look on the server side, the file doesn't exist - nor do I receive an error. The server shows that it received a connection when I send the file to upload. What I have currently is below... Any assistance would be greatly appreciated.
Server.py
import config, protocol
import os
from socket import *
import threading
import time
import sys
class server():
# Constructor: load the server information from config file
def __init__(self):
self.port, self.path, self.path2 = config.config().readServerConfig()
# function to receive file data from client
def receiveFile(self, serverSocket, fileName):
serverSocket.connect()
serverSocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
with open(fileName, 'wb') as f:
print('file incoming...')
while True:
print('receiving data...')
data = serverSocket.recv(1024)
if not data:
break
f.write(data)
print(fileName + " has been Received!")
serverSocket.close()
# Main function of server, start the file sharing service
def start(self):
serverPort=self.port
serverSocket=socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(20)
print('The server is ready to receive')
while True:
connectionSocket, addr = serverSocket.accept()
print("**Conn. to ", addr)
dataRec = connectionSocket.recv(1024)
header,msg= protocol.decodeMsg(dataRec.decode()) # get client's info, parse it to header and content
# Main logic of the program, send different content to client according to client's requests
if(header== protocol.HEAD_REQUEST):
self.listFile(connectionSocket)
elif(header == protocol.HEAD_DOWNLOAD):
self.sendFile(connectionSocket, self.path+"/"+msg)
elif(header == protocol.HEAD_UPLOAD):
self.receiveFile(connectionSocket, self.path2+"/"+msg)
else:
connectionSocket.send(protocol.prepareMsg(protocol.HEAD_ERROR, "Invalid Message"))
connectionSocket.close()
def main():
s=server()
s.start()
main()
Client.py
import config, protocol
from socket import *
import threading
import time
import os
import sys
class client:
fileList= # list to store the file information
uploadFileList =
#Constructor: load client configuration from config file
def __init__(self):
self.serverName, self.serverPort, self.clientPort, self.downloadPath, self.uploadPath = config.config().readClientConfig()
# Function to produce user menu
def printMenu(self):
print("Welcome to simple file sharing system!")
print("Please select operations from menu")
print("--------------------------------------")
print("3. Upload File")
print("5. Quit")
# Function to get user selection from the menu
def getUserSelection(self):
ans=0
# only accept option 1-4
while ans>5 or ans<1:
self.printMenu()
try:
ans=int(input())
except:
ans=0
if (ans<=5) and (ans>=1):
return ans
print("Invalid Option")
# Build connection to server
def connect(self):
serverName = self.serverName
serverPort = self.serverPort
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
return clientSocket
def getUploadFileList(self):
self.uploadFileList = os.listdir(self.uploadPath)
def printUploadFileList(self):
count = 0
for f in self.uploadFileList:
count += 1
print('{:<3d}{}'.format(count, f))
def selectUploadFile(self):
if (len(self.uploadFileList)==0):
self.getUploadFileList()
ans=-1
while ans<0 or ans>len(self.uploadFileList)+1:
self.printUploadFileList()
print("Please select the file you want to upload from the list (enter the number of files):")
try:
ans=int(input())
except:
ans=-1
if (ans>0) and (ans<len(self.uploadFileList)+1):
return self.uploadFileList[ans-1]
print("Invalid number")
def uploadFile(self, fileName):
mySocket=self.connect()
mySocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
f = open(fileName, 'rb')
l = f.read(1024) # each time we only send 1024 bytes of data
while (l):
print('File uploading...')
mySocket.sendall(l)
l = f.read(1024)
f.close()
print("File Uploaded Successfully!")
# Main logic of the client, start the client application
def start(self):
opt=0
while opt!=5:
opt=self.getUserSelection()
if opt==3:
self.uploadFile(self.selectUploadFile())
else:
pass
def main():
c=client()
c.start()
main()
I've defined modules in the protocol file that I have...
Protocol.py
HEAD_LIST='LST'
HEAD_REQUEST='REQ'
HEAD_DOWNLOAD='DLD'
HEAD_UPLOAD='ULD'
HEAD_FILE='FIL'
HEAD_ERROR='ERR'
# we prepare the message that are sent between server and client as the header + content
def prepareMsg(header, msg):
return (header+msg).encode()
def prepareFileList(header,fList):
'''
function to prepare file list to msg
'''
msg=header
for i in range(len(fList)):
if (i==len(fList)-1):
msg+=fList[i]
else:
msg+=fList[i]+','
return msg.encode()
# Decode the received message, the first three letters are used as protocol header
def decodeMsg(msg):
if (len(msg)<=3):
return HEAD_ERROR, 'EMPTY MESSAGE'
else:
return msg[0:3],msg[3:len(msg)]
I've also defined my config file as follows...
Config.py
class config:
#define header
server_port='SERVER_PORT'
path="PATH"
path2="PATH2"
server="SERVER"
client_port="CLIENT_PORT"
download="DOWNLOAD"
upload="UPLOAD"
serverConfig="server.config"
clientConfig="client.config"
def __init__(self):
pass
def readServerConfig(self):
try:
with open(self.serverConfig,'r') as f:
serPort=0
sharePath=""
sharePath2=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.path):
sharePath=sub[1]
elif (sub[0] == self.path2):
sharePath2 = sub[1]
else:
pass
return serPort, sharePath, sharePath2
except:
print(Exception.message())
def readClientConfig(self):
'''
This function read client configuration file, return four values
@return: serverName
@return: serverPort
@return: clientPort
@return: downloadPath
'''
try:
with open(self.clientConfig,'r') as f:
serPort=0
serName=""
clientPort=0
downPath=""
upPath=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.server):
serName=sub[1]
elif(sub[0]==self.client_port):
clientPort=sub[1]
elif(sub[0]==self.download):
downPath=sub[1]
elif(sub[0]==self.upload):
upPath=sub[1]
else:
pass
return serName, serPort, clientPort, downPath, upPath
except:
print(Exception.message())
# The function to test the configuration class
def test():
conf=config()
client=conf.readClientConfig()
server=conf.readServerConfig()
print(client)
print(server)
The above code will return a file list, I can choose which file I'd like to upload, and the code will state its complete but I cannot find the files on the server. I have test files set up in directories paths on both machines. in addition, my server.config and client.config files are set up as:
server.config
SERVER_PORT=12007
PATH=/root/serverFiles
PATH2=/root/files
client.config
SERVER=192.168.10.1
SERVER_PORT=12007
CLIENT_PORT=12006
DOWNLOAD=/root/Downloads
UPLOAD=/root/Upload
python sockets unix filesystems virtual-machine
add a comment |
I'm looking to find some assistance in my python socket programming.
Objective: upload a file from the Client to the Server. My current setup involves two virtual machines, a Server and a Client, where the respective .py files will reside; server.py and client.py . The issues I'm experiencing is, that when I go to choose the upload option, the output reads "File uploading.... File successfully uploaded" But when I look on the server side, the file doesn't exist - nor do I receive an error. The server shows that it received a connection when I send the file to upload. What I have currently is below... Any assistance would be greatly appreciated.
Server.py
import config, protocol
import os
from socket import *
import threading
import time
import sys
class server():
# Constructor: load the server information from config file
def __init__(self):
self.port, self.path, self.path2 = config.config().readServerConfig()
# function to receive file data from client
def receiveFile(self, serverSocket, fileName):
serverSocket.connect()
serverSocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
with open(fileName, 'wb') as f:
print('file incoming...')
while True:
print('receiving data...')
data = serverSocket.recv(1024)
if not data:
break
f.write(data)
print(fileName + " has been Received!")
serverSocket.close()
# Main function of server, start the file sharing service
def start(self):
serverPort=self.port
serverSocket=socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(20)
print('The server is ready to receive')
while True:
connectionSocket, addr = serverSocket.accept()
print("**Conn. to ", addr)
dataRec = connectionSocket.recv(1024)
header,msg= protocol.decodeMsg(dataRec.decode()) # get client's info, parse it to header and content
# Main logic of the program, send different content to client according to client's requests
if(header== protocol.HEAD_REQUEST):
self.listFile(connectionSocket)
elif(header == protocol.HEAD_DOWNLOAD):
self.sendFile(connectionSocket, self.path+"/"+msg)
elif(header == protocol.HEAD_UPLOAD):
self.receiveFile(connectionSocket, self.path2+"/"+msg)
else:
connectionSocket.send(protocol.prepareMsg(protocol.HEAD_ERROR, "Invalid Message"))
connectionSocket.close()
def main():
s=server()
s.start()
main()
Client.py
import config, protocol
from socket import *
import threading
import time
import os
import sys
class client:
fileList= # list to store the file information
uploadFileList =
#Constructor: load client configuration from config file
def __init__(self):
self.serverName, self.serverPort, self.clientPort, self.downloadPath, self.uploadPath = config.config().readClientConfig()
# Function to produce user menu
def printMenu(self):
print("Welcome to simple file sharing system!")
print("Please select operations from menu")
print("--------------------------------------")
print("3. Upload File")
print("5. Quit")
# Function to get user selection from the menu
def getUserSelection(self):
ans=0
# only accept option 1-4
while ans>5 or ans<1:
self.printMenu()
try:
ans=int(input())
except:
ans=0
if (ans<=5) and (ans>=1):
return ans
print("Invalid Option")
# Build connection to server
def connect(self):
serverName = self.serverName
serverPort = self.serverPort
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
return clientSocket
def getUploadFileList(self):
self.uploadFileList = os.listdir(self.uploadPath)
def printUploadFileList(self):
count = 0
for f in self.uploadFileList:
count += 1
print('{:<3d}{}'.format(count, f))
def selectUploadFile(self):
if (len(self.uploadFileList)==0):
self.getUploadFileList()
ans=-1
while ans<0 or ans>len(self.uploadFileList)+1:
self.printUploadFileList()
print("Please select the file you want to upload from the list (enter the number of files):")
try:
ans=int(input())
except:
ans=-1
if (ans>0) and (ans<len(self.uploadFileList)+1):
return self.uploadFileList[ans-1]
print("Invalid number")
def uploadFile(self, fileName):
mySocket=self.connect()
mySocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
f = open(fileName, 'rb')
l = f.read(1024) # each time we only send 1024 bytes of data
while (l):
print('File uploading...')
mySocket.sendall(l)
l = f.read(1024)
f.close()
print("File Uploaded Successfully!")
# Main logic of the client, start the client application
def start(self):
opt=0
while opt!=5:
opt=self.getUserSelection()
if opt==3:
self.uploadFile(self.selectUploadFile())
else:
pass
def main():
c=client()
c.start()
main()
I've defined modules in the protocol file that I have...
Protocol.py
HEAD_LIST='LST'
HEAD_REQUEST='REQ'
HEAD_DOWNLOAD='DLD'
HEAD_UPLOAD='ULD'
HEAD_FILE='FIL'
HEAD_ERROR='ERR'
# we prepare the message that are sent between server and client as the header + content
def prepareMsg(header, msg):
return (header+msg).encode()
def prepareFileList(header,fList):
'''
function to prepare file list to msg
'''
msg=header
for i in range(len(fList)):
if (i==len(fList)-1):
msg+=fList[i]
else:
msg+=fList[i]+','
return msg.encode()
# Decode the received message, the first three letters are used as protocol header
def decodeMsg(msg):
if (len(msg)<=3):
return HEAD_ERROR, 'EMPTY MESSAGE'
else:
return msg[0:3],msg[3:len(msg)]
I've also defined my config file as follows...
Config.py
class config:
#define header
server_port='SERVER_PORT'
path="PATH"
path2="PATH2"
server="SERVER"
client_port="CLIENT_PORT"
download="DOWNLOAD"
upload="UPLOAD"
serverConfig="server.config"
clientConfig="client.config"
def __init__(self):
pass
def readServerConfig(self):
try:
with open(self.serverConfig,'r') as f:
serPort=0
sharePath=""
sharePath2=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.path):
sharePath=sub[1]
elif (sub[0] == self.path2):
sharePath2 = sub[1]
else:
pass
return serPort, sharePath, sharePath2
except:
print(Exception.message())
def readClientConfig(self):
'''
This function read client configuration file, return four values
@return: serverName
@return: serverPort
@return: clientPort
@return: downloadPath
'''
try:
with open(self.clientConfig,'r') as f:
serPort=0
serName=""
clientPort=0
downPath=""
upPath=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.server):
serName=sub[1]
elif(sub[0]==self.client_port):
clientPort=sub[1]
elif(sub[0]==self.download):
downPath=sub[1]
elif(sub[0]==self.upload):
upPath=sub[1]
else:
pass
return serName, serPort, clientPort, downPath, upPath
except:
print(Exception.message())
# The function to test the configuration class
def test():
conf=config()
client=conf.readClientConfig()
server=conf.readServerConfig()
print(client)
print(server)
The above code will return a file list, I can choose which file I'd like to upload, and the code will state its complete but I cannot find the files on the server. I have test files set up in directories paths on both machines. in addition, my server.config and client.config files are set up as:
server.config
SERVER_PORT=12007
PATH=/root/serverFiles
PATH2=/root/files
client.config
SERVER=192.168.10.1
SERVER_PORT=12007
CLIENT_PORT=12006
DOWNLOAD=/root/Downloads
UPLOAD=/root/Upload
python sockets unix filesystems virtual-machine
add a comment |
I'm looking to find some assistance in my python socket programming.
Objective: upload a file from the Client to the Server. My current setup involves two virtual machines, a Server and a Client, where the respective .py files will reside; server.py and client.py . The issues I'm experiencing is, that when I go to choose the upload option, the output reads "File uploading.... File successfully uploaded" But when I look on the server side, the file doesn't exist - nor do I receive an error. The server shows that it received a connection when I send the file to upload. What I have currently is below... Any assistance would be greatly appreciated.
Server.py
import config, protocol
import os
from socket import *
import threading
import time
import sys
class server():
# Constructor: load the server information from config file
def __init__(self):
self.port, self.path, self.path2 = config.config().readServerConfig()
# function to receive file data from client
def receiveFile(self, serverSocket, fileName):
serverSocket.connect()
serverSocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
with open(fileName, 'wb') as f:
print('file incoming...')
while True:
print('receiving data...')
data = serverSocket.recv(1024)
if not data:
break
f.write(data)
print(fileName + " has been Received!")
serverSocket.close()
# Main function of server, start the file sharing service
def start(self):
serverPort=self.port
serverSocket=socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(20)
print('The server is ready to receive')
while True:
connectionSocket, addr = serverSocket.accept()
print("**Conn. to ", addr)
dataRec = connectionSocket.recv(1024)
header,msg= protocol.decodeMsg(dataRec.decode()) # get client's info, parse it to header and content
# Main logic of the program, send different content to client according to client's requests
if(header== protocol.HEAD_REQUEST):
self.listFile(connectionSocket)
elif(header == protocol.HEAD_DOWNLOAD):
self.sendFile(connectionSocket, self.path+"/"+msg)
elif(header == protocol.HEAD_UPLOAD):
self.receiveFile(connectionSocket, self.path2+"/"+msg)
else:
connectionSocket.send(protocol.prepareMsg(protocol.HEAD_ERROR, "Invalid Message"))
connectionSocket.close()
def main():
s=server()
s.start()
main()
Client.py
import config, protocol
from socket import *
import threading
import time
import os
import sys
class client:
fileList= # list to store the file information
uploadFileList =
#Constructor: load client configuration from config file
def __init__(self):
self.serverName, self.serverPort, self.clientPort, self.downloadPath, self.uploadPath = config.config().readClientConfig()
# Function to produce user menu
def printMenu(self):
print("Welcome to simple file sharing system!")
print("Please select operations from menu")
print("--------------------------------------")
print("3. Upload File")
print("5. Quit")
# Function to get user selection from the menu
def getUserSelection(self):
ans=0
# only accept option 1-4
while ans>5 or ans<1:
self.printMenu()
try:
ans=int(input())
except:
ans=0
if (ans<=5) and (ans>=1):
return ans
print("Invalid Option")
# Build connection to server
def connect(self):
serverName = self.serverName
serverPort = self.serverPort
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
return clientSocket
def getUploadFileList(self):
self.uploadFileList = os.listdir(self.uploadPath)
def printUploadFileList(self):
count = 0
for f in self.uploadFileList:
count += 1
print('{:<3d}{}'.format(count, f))
def selectUploadFile(self):
if (len(self.uploadFileList)==0):
self.getUploadFileList()
ans=-1
while ans<0 or ans>len(self.uploadFileList)+1:
self.printUploadFileList()
print("Please select the file you want to upload from the list (enter the number of files):")
try:
ans=int(input())
except:
ans=-1
if (ans>0) and (ans<len(self.uploadFileList)+1):
return self.uploadFileList[ans-1]
print("Invalid number")
def uploadFile(self, fileName):
mySocket=self.connect()
mySocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
f = open(fileName, 'rb')
l = f.read(1024) # each time we only send 1024 bytes of data
while (l):
print('File uploading...')
mySocket.sendall(l)
l = f.read(1024)
f.close()
print("File Uploaded Successfully!")
# Main logic of the client, start the client application
def start(self):
opt=0
while opt!=5:
opt=self.getUserSelection()
if opt==3:
self.uploadFile(self.selectUploadFile())
else:
pass
def main():
c=client()
c.start()
main()
I've defined modules in the protocol file that I have...
Protocol.py
HEAD_LIST='LST'
HEAD_REQUEST='REQ'
HEAD_DOWNLOAD='DLD'
HEAD_UPLOAD='ULD'
HEAD_FILE='FIL'
HEAD_ERROR='ERR'
# we prepare the message that are sent between server and client as the header + content
def prepareMsg(header, msg):
return (header+msg).encode()
def prepareFileList(header,fList):
'''
function to prepare file list to msg
'''
msg=header
for i in range(len(fList)):
if (i==len(fList)-1):
msg+=fList[i]
else:
msg+=fList[i]+','
return msg.encode()
# Decode the received message, the first three letters are used as protocol header
def decodeMsg(msg):
if (len(msg)<=3):
return HEAD_ERROR, 'EMPTY MESSAGE'
else:
return msg[0:3],msg[3:len(msg)]
I've also defined my config file as follows...
Config.py
class config:
#define header
server_port='SERVER_PORT'
path="PATH"
path2="PATH2"
server="SERVER"
client_port="CLIENT_PORT"
download="DOWNLOAD"
upload="UPLOAD"
serverConfig="server.config"
clientConfig="client.config"
def __init__(self):
pass
def readServerConfig(self):
try:
with open(self.serverConfig,'r') as f:
serPort=0
sharePath=""
sharePath2=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.path):
sharePath=sub[1]
elif (sub[0] == self.path2):
sharePath2 = sub[1]
else:
pass
return serPort, sharePath, sharePath2
except:
print(Exception.message())
def readClientConfig(self):
'''
This function read client configuration file, return four values
@return: serverName
@return: serverPort
@return: clientPort
@return: downloadPath
'''
try:
with open(self.clientConfig,'r') as f:
serPort=0
serName=""
clientPort=0
downPath=""
upPath=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.server):
serName=sub[1]
elif(sub[0]==self.client_port):
clientPort=sub[1]
elif(sub[0]==self.download):
downPath=sub[1]
elif(sub[0]==self.upload):
upPath=sub[1]
else:
pass
return serName, serPort, clientPort, downPath, upPath
except:
print(Exception.message())
# The function to test the configuration class
def test():
conf=config()
client=conf.readClientConfig()
server=conf.readServerConfig()
print(client)
print(server)
The above code will return a file list, I can choose which file I'd like to upload, and the code will state its complete but I cannot find the files on the server. I have test files set up in directories paths on both machines. in addition, my server.config and client.config files are set up as:
server.config
SERVER_PORT=12007
PATH=/root/serverFiles
PATH2=/root/files
client.config
SERVER=192.168.10.1
SERVER_PORT=12007
CLIENT_PORT=12006
DOWNLOAD=/root/Downloads
UPLOAD=/root/Upload
python sockets unix filesystems virtual-machine
I'm looking to find some assistance in my python socket programming.
Objective: upload a file from the Client to the Server. My current setup involves two virtual machines, a Server and a Client, where the respective .py files will reside; server.py and client.py . The issues I'm experiencing is, that when I go to choose the upload option, the output reads "File uploading.... File successfully uploaded" But when I look on the server side, the file doesn't exist - nor do I receive an error. The server shows that it received a connection when I send the file to upload. What I have currently is below... Any assistance would be greatly appreciated.
Server.py
import config, protocol
import os
from socket import *
import threading
import time
import sys
class server():
# Constructor: load the server information from config file
def __init__(self):
self.port, self.path, self.path2 = config.config().readServerConfig()
# function to receive file data from client
def receiveFile(self, serverSocket, fileName):
serverSocket.connect()
serverSocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
with open(fileName, 'wb') as f:
print('file incoming...')
while True:
print('receiving data...')
data = serverSocket.recv(1024)
if not data:
break
f.write(data)
print(fileName + " has been Received!")
serverSocket.close()
# Main function of server, start the file sharing service
def start(self):
serverPort=self.port
serverSocket=socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(20)
print('The server is ready to receive')
while True:
connectionSocket, addr = serverSocket.accept()
print("**Conn. to ", addr)
dataRec = connectionSocket.recv(1024)
header,msg= protocol.decodeMsg(dataRec.decode()) # get client's info, parse it to header and content
# Main logic of the program, send different content to client according to client's requests
if(header== protocol.HEAD_REQUEST):
self.listFile(connectionSocket)
elif(header == protocol.HEAD_DOWNLOAD):
self.sendFile(connectionSocket, self.path+"/"+msg)
elif(header == protocol.HEAD_UPLOAD):
self.receiveFile(connectionSocket, self.path2+"/"+msg)
else:
connectionSocket.send(protocol.prepareMsg(protocol.HEAD_ERROR, "Invalid Message"))
connectionSocket.close()
def main():
s=server()
s.start()
main()
Client.py
import config, protocol
from socket import *
import threading
import time
import os
import sys
class client:
fileList= # list to store the file information
uploadFileList =
#Constructor: load client configuration from config file
def __init__(self):
self.serverName, self.serverPort, self.clientPort, self.downloadPath, self.uploadPath = config.config().readClientConfig()
# Function to produce user menu
def printMenu(self):
print("Welcome to simple file sharing system!")
print("Please select operations from menu")
print("--------------------------------------")
print("3. Upload File")
print("5. Quit")
# Function to get user selection from the menu
def getUserSelection(self):
ans=0
# only accept option 1-4
while ans>5 or ans<1:
self.printMenu()
try:
ans=int(input())
except:
ans=0
if (ans<=5) and (ans>=1):
return ans
print("Invalid Option")
# Build connection to server
def connect(self):
serverName = self.serverName
serverPort = self.serverPort
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
return clientSocket
def getUploadFileList(self):
self.uploadFileList = os.listdir(self.uploadPath)
def printUploadFileList(self):
count = 0
for f in self.uploadFileList:
count += 1
print('{:<3d}{}'.format(count, f))
def selectUploadFile(self):
if (len(self.uploadFileList)==0):
self.getUploadFileList()
ans=-1
while ans<0 or ans>len(self.uploadFileList)+1:
self.printUploadFileList()
print("Please select the file you want to upload from the list (enter the number of files):")
try:
ans=int(input())
except:
ans=-1
if (ans>0) and (ans<len(self.uploadFileList)+1):
return self.uploadFileList[ans-1]
print("Invalid number")
def uploadFile(self, fileName):
mySocket=self.connect()
mySocket.send(protocol.prepareMsg(protocol.HEAD_UPLOAD, fileName))
f = open(fileName, 'rb')
l = f.read(1024) # each time we only send 1024 bytes of data
while (l):
print('File uploading...')
mySocket.sendall(l)
l = f.read(1024)
f.close()
print("File Uploaded Successfully!")
# Main logic of the client, start the client application
def start(self):
opt=0
while opt!=5:
opt=self.getUserSelection()
if opt==3:
self.uploadFile(self.selectUploadFile())
else:
pass
def main():
c=client()
c.start()
main()
I've defined modules in the protocol file that I have...
Protocol.py
HEAD_LIST='LST'
HEAD_REQUEST='REQ'
HEAD_DOWNLOAD='DLD'
HEAD_UPLOAD='ULD'
HEAD_FILE='FIL'
HEAD_ERROR='ERR'
# we prepare the message that are sent between server and client as the header + content
def prepareMsg(header, msg):
return (header+msg).encode()
def prepareFileList(header,fList):
'''
function to prepare file list to msg
'''
msg=header
for i in range(len(fList)):
if (i==len(fList)-1):
msg+=fList[i]
else:
msg+=fList[i]+','
return msg.encode()
# Decode the received message, the first three letters are used as protocol header
def decodeMsg(msg):
if (len(msg)<=3):
return HEAD_ERROR, 'EMPTY MESSAGE'
else:
return msg[0:3],msg[3:len(msg)]
I've also defined my config file as follows...
Config.py
class config:
#define header
server_port='SERVER_PORT'
path="PATH"
path2="PATH2"
server="SERVER"
client_port="CLIENT_PORT"
download="DOWNLOAD"
upload="UPLOAD"
serverConfig="server.config"
clientConfig="client.config"
def __init__(self):
pass
def readServerConfig(self):
try:
with open(self.serverConfig,'r') as f:
serPort=0
sharePath=""
sharePath2=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.path):
sharePath=sub[1]
elif (sub[0] == self.path2):
sharePath2 = sub[1]
else:
pass
return serPort, sharePath, sharePath2
except:
print(Exception.message())
def readClientConfig(self):
'''
This function read client configuration file, return four values
@return: serverName
@return: serverPort
@return: clientPort
@return: downloadPath
'''
try:
with open(self.clientConfig,'r') as f:
serPort=0
serName=""
clientPort=0
downPath=""
upPath=""
for l in f:
sub=l.strip().split("=")
if(sub[0]==self.server_port):
serPort=int(sub[1])
elif(sub[0]==self.server):
serName=sub[1]
elif(sub[0]==self.client_port):
clientPort=sub[1]
elif(sub[0]==self.download):
downPath=sub[1]
elif(sub[0]==self.upload):
upPath=sub[1]
else:
pass
return serName, serPort, clientPort, downPath, upPath
except:
print(Exception.message())
# The function to test the configuration class
def test():
conf=config()
client=conf.readClientConfig()
server=conf.readServerConfig()
print(client)
print(server)
The above code will return a file list, I can choose which file I'd like to upload, and the code will state its complete but I cannot find the files on the server. I have test files set up in directories paths on both machines. in addition, my server.config and client.config files are set up as:
server.config
SERVER_PORT=12007
PATH=/root/serverFiles
PATH2=/root/files
client.config
SERVER=192.168.10.1
SERVER_PORT=12007
CLIENT_PORT=12006
DOWNLOAD=/root/Downloads
UPLOAD=/root/Upload
python sockets unix filesystems virtual-machine
python sockets unix filesystems virtual-machine
edited Nov 17 '18 at 20:25
asked Nov 17 '18 at 17:58
user3600460
186
186
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your uploadFile()
method will connect()
to the server and then sendall()
the file content (in 1024 byte chunks).
Your server, on the other hand, will receive the first 1024 bytes (i.e. the first 1024 bytes of file content), and interpret it according to the protocol, looking at the first three bytes of the file content. The client, however, never sends protocol.HEAD_UPLOAD
as the server expects.
(BTW, I would really recommend you PEP8 the code, and refrain from *
imports. It makes the code much easier to read, and thus to help.)
Thanks!, that helped. Explains why the server wouldn't respond. Unfortunately, I'm getting different errors, but all that means is that i'm making progress.
– user3600460
Nov 17 '18 at 20:01
What errors specifically?
– digitalarbeiter
Nov 17 '18 at 20:06
right now after I make the suggested edits, I see that the file upload is now working successfully - but after the file uploads, the server throws an error:" socket.error: [Errno 104] Connection reset by peer " I'm trying to research the error now. What I've found is that this is the equivalent to EOF
– user3600460
Nov 17 '18 at 20:14
1
At the end ofuploadFile()
, the client socket goes out of scope and is disposed of. The TCP connection is properly closed, informing the server side (the client sending the server aFIN
ish message). That causes the server-side "connection reset by peer" message. To fix this properly, you need to do two things: a) think about the lifecycle of the client-side connection, and b) the server must be prepared for disconnecting clients, anyway.
– digitalarbeiter
Nov 17 '18 at 20:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353987%2fpython-socket-programming-upload-files-from-client-to-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your uploadFile()
method will connect()
to the server and then sendall()
the file content (in 1024 byte chunks).
Your server, on the other hand, will receive the first 1024 bytes (i.e. the first 1024 bytes of file content), and interpret it according to the protocol, looking at the first three bytes of the file content. The client, however, never sends protocol.HEAD_UPLOAD
as the server expects.
(BTW, I would really recommend you PEP8 the code, and refrain from *
imports. It makes the code much easier to read, and thus to help.)
Thanks!, that helped. Explains why the server wouldn't respond. Unfortunately, I'm getting different errors, but all that means is that i'm making progress.
– user3600460
Nov 17 '18 at 20:01
What errors specifically?
– digitalarbeiter
Nov 17 '18 at 20:06
right now after I make the suggested edits, I see that the file upload is now working successfully - but after the file uploads, the server throws an error:" socket.error: [Errno 104] Connection reset by peer " I'm trying to research the error now. What I've found is that this is the equivalent to EOF
– user3600460
Nov 17 '18 at 20:14
1
At the end ofuploadFile()
, the client socket goes out of scope and is disposed of. The TCP connection is properly closed, informing the server side (the client sending the server aFIN
ish message). That causes the server-side "connection reset by peer" message. To fix this properly, you need to do two things: a) think about the lifecycle of the client-side connection, and b) the server must be prepared for disconnecting clients, anyway.
– digitalarbeiter
Nov 17 '18 at 20:49
add a comment |
Your uploadFile()
method will connect()
to the server and then sendall()
the file content (in 1024 byte chunks).
Your server, on the other hand, will receive the first 1024 bytes (i.e. the first 1024 bytes of file content), and interpret it according to the protocol, looking at the first three bytes of the file content. The client, however, never sends protocol.HEAD_UPLOAD
as the server expects.
(BTW, I would really recommend you PEP8 the code, and refrain from *
imports. It makes the code much easier to read, and thus to help.)
Thanks!, that helped. Explains why the server wouldn't respond. Unfortunately, I'm getting different errors, but all that means is that i'm making progress.
– user3600460
Nov 17 '18 at 20:01
What errors specifically?
– digitalarbeiter
Nov 17 '18 at 20:06
right now after I make the suggested edits, I see that the file upload is now working successfully - but after the file uploads, the server throws an error:" socket.error: [Errno 104] Connection reset by peer " I'm trying to research the error now. What I've found is that this is the equivalent to EOF
– user3600460
Nov 17 '18 at 20:14
1
At the end ofuploadFile()
, the client socket goes out of scope and is disposed of. The TCP connection is properly closed, informing the server side (the client sending the server aFIN
ish message). That causes the server-side "connection reset by peer" message. To fix this properly, you need to do two things: a) think about the lifecycle of the client-side connection, and b) the server must be prepared for disconnecting clients, anyway.
– digitalarbeiter
Nov 17 '18 at 20:49
add a comment |
Your uploadFile()
method will connect()
to the server and then sendall()
the file content (in 1024 byte chunks).
Your server, on the other hand, will receive the first 1024 bytes (i.e. the first 1024 bytes of file content), and interpret it according to the protocol, looking at the first three bytes of the file content. The client, however, never sends protocol.HEAD_UPLOAD
as the server expects.
(BTW, I would really recommend you PEP8 the code, and refrain from *
imports. It makes the code much easier to read, and thus to help.)
Your uploadFile()
method will connect()
to the server and then sendall()
the file content (in 1024 byte chunks).
Your server, on the other hand, will receive the first 1024 bytes (i.e. the first 1024 bytes of file content), and interpret it according to the protocol, looking at the first three bytes of the file content. The client, however, never sends protocol.HEAD_UPLOAD
as the server expects.
(BTW, I would really recommend you PEP8 the code, and refrain from *
imports. It makes the code much easier to read, and thus to help.)
answered Nov 17 '18 at 19:03
digitalarbeiter
1,6301112
1,6301112
Thanks!, that helped. Explains why the server wouldn't respond. Unfortunately, I'm getting different errors, but all that means is that i'm making progress.
– user3600460
Nov 17 '18 at 20:01
What errors specifically?
– digitalarbeiter
Nov 17 '18 at 20:06
right now after I make the suggested edits, I see that the file upload is now working successfully - but after the file uploads, the server throws an error:" socket.error: [Errno 104] Connection reset by peer " I'm trying to research the error now. What I've found is that this is the equivalent to EOF
– user3600460
Nov 17 '18 at 20:14
1
At the end ofuploadFile()
, the client socket goes out of scope and is disposed of. The TCP connection is properly closed, informing the server side (the client sending the server aFIN
ish message). That causes the server-side "connection reset by peer" message. To fix this properly, you need to do two things: a) think about the lifecycle of the client-side connection, and b) the server must be prepared for disconnecting clients, anyway.
– digitalarbeiter
Nov 17 '18 at 20:49
add a comment |
Thanks!, that helped. Explains why the server wouldn't respond. Unfortunately, I'm getting different errors, but all that means is that i'm making progress.
– user3600460
Nov 17 '18 at 20:01
What errors specifically?
– digitalarbeiter
Nov 17 '18 at 20:06
right now after I make the suggested edits, I see that the file upload is now working successfully - but after the file uploads, the server throws an error:" socket.error: [Errno 104] Connection reset by peer " I'm trying to research the error now. What I've found is that this is the equivalent to EOF
– user3600460
Nov 17 '18 at 20:14
1
At the end ofuploadFile()
, the client socket goes out of scope and is disposed of. The TCP connection is properly closed, informing the server side (the client sending the server aFIN
ish message). That causes the server-side "connection reset by peer" message. To fix this properly, you need to do two things: a) think about the lifecycle of the client-side connection, and b) the server must be prepared for disconnecting clients, anyway.
– digitalarbeiter
Nov 17 '18 at 20:49
Thanks!, that helped. Explains why the server wouldn't respond. Unfortunately, I'm getting different errors, but all that means is that i'm making progress.
– user3600460
Nov 17 '18 at 20:01
Thanks!, that helped. Explains why the server wouldn't respond. Unfortunately, I'm getting different errors, but all that means is that i'm making progress.
– user3600460
Nov 17 '18 at 20:01
What errors specifically?
– digitalarbeiter
Nov 17 '18 at 20:06
What errors specifically?
– digitalarbeiter
Nov 17 '18 at 20:06
right now after I make the suggested edits, I see that the file upload is now working successfully - but after the file uploads, the server throws an error:" socket.error: [Errno 104] Connection reset by peer " I'm trying to research the error now. What I've found is that this is the equivalent to EOF
– user3600460
Nov 17 '18 at 20:14
right now after I make the suggested edits, I see that the file upload is now working successfully - but after the file uploads, the server throws an error:" socket.error: [Errno 104] Connection reset by peer " I'm trying to research the error now. What I've found is that this is the equivalent to EOF
– user3600460
Nov 17 '18 at 20:14
1
1
At the end of
uploadFile()
, the client socket goes out of scope and is disposed of. The TCP connection is properly closed, informing the server side (the client sending the server a FIN
ish message). That causes the server-side "connection reset by peer" message. To fix this properly, you need to do two things: a) think about the lifecycle of the client-side connection, and b) the server must be prepared for disconnecting clients, anyway.– digitalarbeiter
Nov 17 '18 at 20:49
At the end of
uploadFile()
, the client socket goes out of scope and is disposed of. The TCP connection is properly closed, informing the server side (the client sending the server a FIN
ish message). That causes the server-side "connection reset by peer" message. To fix this properly, you need to do two things: a) think about the lifecycle of the client-side connection, and b) the server must be prepared for disconnecting clients, anyway.– digitalarbeiter
Nov 17 '18 at 20:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53353987%2fpython-socket-programming-upload-files-from-client-to-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown