在python中将其发送到tcp服务器后的空文件

时间:2016-10-22 20:28:15

标签: python tcp

我是python的新手,我正在尝试使多线程tcp服务器和客户端能够在它们之间发送文件。我确实为这两个程序编写了一些简单的代码,但每次我在服务器的网站上都得到空文件。该文件确实在文件夹中创建,但是当我打开它时它内部是空白的。我也尝试发送.png文件,但是windows photoviewer并没有打开它们说它们是空的。我没有找到任何人提出这样的问题,这就是我要问的原因

Client.py

import socket  # Import socket module

HOST = "localhost"  # Host address / name
PORT = 2137  # Reserves port for the service

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
client.connect((HOST, PORT))

fileToSend = open('test.txt', 'rb') 
print "File ready to be sent" 
l = fileToSend.read(1024)

while l:
    print "Sending the file"
    client.send(l)
    l = fileToSend.read(1024)

fileToSend.close() print "done" 
client.close()

Server.py

import socket
from threading import Thread
from SocketServer import ThreadingMixIn

import sys

TCPHOST = "localhost"
TCPPORT = 2137
BUFFER_SIZE = 20


class ClientThread(Thread):
    def __init__(self, HOST, PORT):
        Thread.__init__(self)
        self.HOST = HOST
        self.PORT = PORT
        print "New thread started for " + HOST + " on port " + str(PORT)

    def run(self):
        f = open('received.py', 'wb')
        while True:

                try:

                    data = conn.recv(1024)

                except socket.error, e:
                    print "Error receiving data: %s" % e
                    sys.exit(1)

                while data:
                    print "Receiving"
                    f.write(data)
                    data = conn.recv(1024)
                f.close()


try:
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((TCPHOST, TCPPORT))
    print "Socket created"
except socket.error, err:
    print "Failed to create socket" % err


threads = []

while True:
    server.listen(4)
    print "Waiting for connections"
    (conn, (HOST, PORT)) = server.accept()
    thread = ClientThread(HOST, PORT)
    thread.start()
    threads.append(thread)

for t in threads:
    t.join()

1 个答案:

答案 0 :(得分:0)

我不确定你真正想做什么,因为我看到你导入了SocketServer,但你并没有全部使用它。

如果您尝试运行一个简单的套接字服务器,则不需要ClientThread类和该文件中有关线程的所有其他内容。

server.py 中的以下代码将执行此任务

import socket

import sys

TCPHOST = "localhost"
TCPPORT = 2137
BUFFER_SIZE = 20

try:
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((TCPHOST, TCPPORT))
    server.listen(4)
    print "Socket created"
except socket.error, err:
    print "Failed to create socket" % err

while True:
        print "Waiting for connections"
        (conn, (TCPHOST, TCPPORT)) = server.accept()
        try:

            while True:
                data = conn.recv(1024)
                f = open('received.py', 'wb')
                if data:
                   print "Receiving " + data
                   f.write(data)
                else:
                   f.close()
                   break;


        except socket.error, e:
            #pass
            print "Error receiving data: %s" % e
                #sys.exit(1)

        finally:
            conn.close()

但是,如果您尝试使用ThreadingMixIn实现线程化的TCPServer,则需要创建一个子类SocketServer并覆盖其handle()函数的类

Python文档对此非常有帮助 https://docs.python.org/3.5/library/socketserver.html

(ThreadingMixin位于页面底部)