相关疑难解决方法(0)

在Python中通过TCP套接字发送文件

我已经成功地将文件内容(图像)复制到新文件中.但是,当我在TCP套接字上尝试相同的事情时,我遇到了问题.服务器循环未退出.客户端循环在到达EOF时退出,但服务器无法识别EOF.

这是代码:

服务器

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
f = open('torecv.png','wb')
s.listen(5)                 # Now wait for client connection.
while True:
    c, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print "Receiving..."
    l = c.recv(1024)
    while (l):
        print "Receiving..."
        f.write(l)
        l = …
Run Code Online (Sandbox Code Playgroud)

python sockets tcp file python-2.7

37
推荐指数
2
解决办法
9万
查看次数

标签 统计

file ×1

python ×1

python-2.7 ×1

sockets ×1

tcp ×1