无法从python中的客户端接收文件

时间:2012-08-26 20:00:43

标签: python sockets

我正在尝试对编译服务器进行编程,编译服务器编译客户端发送的C程序并返回一个目标文件,然后可以在客户端进行链接和执行。这是我的客户端和服务器程序

client.py:

# Compilation client program

import sys, socket, string


File = raw_input("Enter the file name:") 
ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock.connect(('localhost', 5000))

csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csock.connect(('localhost', 5001))

f = open(File, "rb")
data = f.read()
f.close()
ssock.send(File)                 #send filename
ssock.send(data)                 #send file
fd=raw_input("Enter a key to start recieving object file:") 

data=csock.recv(1024)            #receive status
if data=="sucess\n":
    File=File.replace(".c",".o") #objectfile name
    print "Object file, "+File+", recieved sucessfully"
else:
    print "There are compilation errors in " + File
    File="error.txt"             #errorfile name
    print "Errors are reported in the file error.txt"

fobj=open(File,"wb")
while 1:
    data=ssock.recv(1024)        # if any error in c sourcefile then  error gets                                         
                                 # eported in errorfile "error.txt" else objectfile is 
                                 # returned from server
    if not data:break
    fobj.write(data)
fobj.close()
ssock.close()
csock.close()

server.py

#Compilation Server program

import subprocess
import socket, time, string, sys, urlparse, os
ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock.bind(('', 5000))                   
ssock.listen(2)
print 'Server Listening on port 5000'
csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csock.bind(('', 5001))
csock.listen(2)
print 'Control server listening on port 5001'
client, claddr = ssock.accept()
controlsoc, caddr = csock.accept()


filename=client.recv(1024)        #receive filename
print filename
###############  This code is not working, i'm not getting the reason   #######
###############    I want to receive a file more than 1KB from client   #######
f = open(filename,"wb")           #receive file======
while 1:
    data = client.recv(1024)
    if not data: break
    f.write(data)
f.close()
###############
###############


data="gcc -c " + filename + " 2> error.txt"  #shell command to execute c source file 
                                             #report errors if any to error.txt    
from subprocess import call

call(data,shell=True)                        #executes the above shell command
fil = filename.replace(".c",".o")
if (os.path.isfile(fil))== True:             #test for existence of objectfile 
    data = "sucess\n"                        #no objectfile => error in compilation
    filename = filename.replace(".c",".o")
else:
    data = "unsucessful\n"
    print data+"hi"
    filename = "error.txt"

controlsoc.send(data)
f = open(filename,"rb")
data=f.read()
f.close()
print data

client.send(data)

client.close()
controlsoc.close()

我无法接收多个KB的文件。我的代码中是否存在任何缺陷,或者我应该如何修改代码以实现编译编译服务器的目标。 请帮我这方面..谢谢提前

1 个答案:

答案 0 :(得分:1)

这里的问题是你假设ssock.send(File)会导致filename=client.recv(1024)完全读取文件名而不是更多,但实际上接收方不知道文件名在哪里结束而你最终得到了文件名 filename变量中的部分数据。

TCP连接是双向字节流。它不知道您的消息的边界。一个send可能对应于另一侧的多个recv(反之亦然)。您需要在原始TCP之上的应用程序级协议

在您的情况下,最简单的方法是以file-size file-name\n格式发送文本行作为标题。这样,您的服务器不仅可以将标头与文件数据分开(通过换行),还可以知道要预期的文件内容的字节数,并为多个文件重用相同的TCP连接。