I'm learning socket programming and python. I need to create a server that accepts a command from client and send a large text file back to the client. The client then saves the complete text file to its directory. In my code, the server is sending the whole text file line by line, but the client could only accept 1024 bytes. I'm not sure what I need to add so the client can receive the whole text file from the server and save it to its directory. Can someone take a look at my code and point me to the right direction? Your help is greatly appreciated.
server.py
import socket
import sys
import os
from thread import *
HOST = ''
PORT = 8888
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
server_socket.bind((HOST, PORT)) #bind to a address(and port)
except socket.error, msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
#put the socket in listening mode
server_socket.listen(10) #maximum 10 connections
print 'TCP Server Waiting for client on port 30021'
#wait to accept a connection - blocking call
client, addr = server_socket.accept()
#display client information
print 'Connected with ' + addr[0] + ':' + str(addr[1])
try:
#keep talking with the client
while 1:
#Receiving from client
data = client.recv(1024)
#command: list
commandlist = data.split()
if (commandlist[0].strip() in ('list')):
reply = 'Directory: ' + os.getcwd() #current directory
client.send(reply)
elif (commandlist[0] == 'get' and len(commandlist) >= 2):
filename = commandlist[1]
reply = filename
#validate filename
if os.path.exists(filename):
length = os.path.getsize(filename)
with open(filename, 'r') as infile:
for line in infile:
reply = line
client.sendall(reply)
#f = open(filename, 'r')
#reply = f.read()
#client.send(piece)
#f.close()
else:
reply = 'File not found'
client.send(reply)
elif (commandlist[0] == 'get' and len(commandlist) < 2):
#if the command argument is less than 2
reply = 'Provide a filename please'
client.send(reply)
else:
reply = 'Error: Wrong command'
client.send(reply)
except KeyboardInterrupt:
print "Exiting gracefully."
finally:
server_socket.close()
client.py
#Socket clicent in python
import socket #for sockets
import sys #for exit
command = ' '
socksize = 1024
#return a socket descriptor which can be used in other socket related functions
#properties: address family: AF_INET (IP v4)
#properties: type: SOCK_STREAM (connection oriented TCP protocol)
try:
#create an AF_INET, STREAM socket (TCP)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg: #error handling
print 'Failed to create socket. Error code: ' + str(msg[0]) + ', Error message: ' + msg[1]
sys.exit();
print 'Socket Created'
#Get the IP address of the remote host/url
#connect to IP on a certain 'port' using the connect
host = '' #symbolic name meaning the local host
port = 8888 #arbitrary non-privileged port
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
#could not resolve
print 'Hostname could not be resolved. Existing'
sys.exit()
print 'IP address of ' + host + ' is ' + remote_ip
#Connect to remote server
client_socket.connect((remote_ip, port))
print 'Socket Connected to ' + host + ' on ip ' + remote_ip
buf = ''
#Send some data to remote server
while True:
print 'Enter a command: list or get <filename>'
command = raw_input()
if command.strip() == 'quit':
break
client_socket.send(command)
data = client_socket.recv(socksize)
print data
#Close the socket
client_socket.close()