Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过TCP套接字发送文件_Python_Sockets_Tcp - Fatal编程技术网

Python 通过TCP套接字发送文件

Python 通过TCP套接字发送文件,python,sockets,tcp,Python,Sockets,Tcp,我是编程初学者 我有一个程序,可以从web通过python中的TCP套接字发送.wav文件。程序运行良好,但我有一个问题。当服务器和客户端之间的传输完成时,服务器端的程序仍在运行。有没有办法“杀了他”?我的意思是转移完成后,程序将自行结束。 对不起,英语不好。 以下是代码: import socket # Import socket module import os port = 60001 # Reserve a port

我是编程初学者 我有一个程序,可以从web通过python中的TCP套接字发送.wav文件。程序运行良好,但我有一个问题。当服务器和客户端之间的传输完成时,服务器端的程序仍在运行。有没有办法“杀了他”?我的意思是转移完成后,程序将自行结束。 对不起,英语不好。 以下是代码:

import socket                   # Import socket module
import os

port = 60001                  # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()


# Get local machine name
s.bind((host, port))
f = open('fare.wav', 'wb')
# Bind to the port
s.listen(1)                     # Now wait for client connection.

print 'Server listening....'

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print "Recieving..."
    l = conn.recv(4096)
    while (l):
       print "Recieving..."
       f.write(l)
       l = conn.recv(4096)
    f.close()
    print('Done receiving')
    conn.send('Thank you for connecting')
    conn.close()
客户:

import os
import socket
s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 60001 # Reserve a port for your service.

s.connect((host, port))

f = open('fanfare2.wav','rb')
print("Sending...")
l = f.read(4096)
while (l):
    print("Sending...")
    s.send(l)
    l = f.read(4096)
f.close()
print("Done Sending")
s.shutdown(socket.SHUT_WR)
print s.recv(1024)
s.close()

如果您不希望它在完成事务后关闭,只需使用break退出服务器代码上的while循环,也请查看


如果您不想让程序永远运行,为什么要使用
while True
循环?删除该循环或随时打断它。请正确缩进代码,很难看到您在这里做什么。@georg,这不仅很难,而且在Python中语法不正确。:)非常感谢@timgeb
#!/usr/bin/python

import socket                   # Import socket module
import os

port = 60001                  # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()


# Get local machine name
s.bind((host, port))
f = open('fare.wav', 'wb')
# Bind to the port
s.listen(1)                     # Now wait for client connection.

print 'Server listening....'

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print "Recieving..."
    l = conn.recv(4096)
    while (l):
       print "Recieving..."
       f.write(l)
       l = conn.recv(4096)
    f.close()
    print('Done receiving')
    conn.send('Thank you for connecting')
    conn.close()
    break