摆烂了摆烂了 2021-10-29 10:41 采纳率: 100%
浏览 106
已结题

python用tcp写server传输文件遇到的问题

我被要求写包括client,和server在内的三个文件来在本地实现文件传输(通过cmd),
但是我的另一个文件(我取名为Others.py)和client.py好像有问题:
代码如下

# server.py
import socket
import sys
import Others

# Create socket, AF_INET corresponds to IPv4, and SOCK_STREAM corresponds to TCP
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind to the port
tcp_socket.bind(('', int(sys.argv[1])))

# Convert active socket to passive socket
tcp_socket.listen(5)
print("Server is listening:")
# The combination of an IPv4 address and a port number is known as the socket number
# A pair of sockets, one socket at the client side and other socket at the server side,
# define the TCP/UDP connection end points
# A socket number can uniquely identify a network resource in the whole internet.
# From https://www.omnisecu.com/tcpip/what-are-port-and-socket-numbers.php

while True:
    # Use accept to get the address of the sub-socket and the client
    client_socket, address = tcp_socket.accept()

    print(f"connection from {address}has been established")

    # send message
    client_socket.send(bytes(f"Welcome to this server!", "utf-8"))
    Others.recv_file(client_socket, 'my.txt')
    client_socket.close()
# client.py
import socket
import sys
import Others

#def main():
# Create socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      
ip = sys.argv[1]
# The port for my laptop
port = int(sys.argv[2])

# connect to the server
tcp_socket.connect((ip, port))
full_msg = ''

filename = input("please type in your file name \n")
tcp_socket.send(bytes(filename,'utf-8'))

while True:
    print('receiving...')

    # Receive the content returned by the server
    msg = tcp_socket.recv(4)

    if msg:
        break
    full_msg += msg.decode("utf-8")

print(full_msg)

Others.send_file(tcp_socket, "C:/Users/ASUS/Desktop/123.txt")

tcp_socket.close()
print('connection closed')
# Others.py
def send_file(socket , filename):
    name = filename

    socket.send(bytes(f"{name}", 'utf-8'))
    # read in binary mode
    file = open(filename, 'rb')
    socket.send(file)


def recv_file(socket, filename):
    name = filename
    # write in binary mode
    file = open(name, 'wb')
    s = socket.recv(1024)
    while s:
        file.write(s)
        s = socket.recv(1024)
    file.close()

报错如下

#这是cmd的提示
Traceback (most recent call last):
  File "C:\Users\ASUS\Desktop\myTCPserver\client.py", line 33, in <module>
    Others.send_file(tcp_socket, "C:/Users/ASUS/Desktop/123.txt")
  File "C:\Users\ASUS\Desktop\myTCPserver\Others.py", line 64, in send_file
    socket.send(file)
TypeError: a bytes-like object is required, not '_io.BufferedReader'

C:\Users\ASUS\Desktop\myTCPserver>

#这是cmd的提示
C:\Users\ASUS\Desktop\myTCPserver>python server.py 6789
Server is listening:
connection from ('127.0.0.1', 55603)has been established
Traceback (most recent call last):
  File "C:\Users\ASUS\Desktop\myTCPserver\server.py", line 28, in <module>
    Others.recv_file(client_socket, 'my.txt')
  File "C:\Users\ASUS\Desktop\myTCPserver\Others.py", line 74, in recv_file
    s = socket.recv(1024)
ConnectionResetError: [WinError 10054] 远程主机强迫关闭了一个现有的连接。

C:\Users\ASUS\Desktop\myTCPserver>

不知道怎么改,不改变大概结构。有没有人看看

  • 写回答

3条回答 默认 最新

  • soar3033 2021-10-29 13:42
    关注

    错误的确不少,已经都给你改过来了,也运行成功了,请采纳

    客户端

    
    
    # server.py
    import socket
    import sys
    import Others
    # Create socket, AF_INET corresponds to IPv4, and SOCK_STREAM corresponds to TCP
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # bind to the port
    tcp_socket.bind(('', int(sys.argv[1])))
    # Convert active socket to passive socket
    tcp_socket.listen(5)
    print("Server is listening:")
    # The combination of an IPv4 address and a port number is known as the socket number
    # A pair of sockets, one socket at the client side and other socket at the server side,
    # define the TCP/UDP connection end points
    # A socket number can uniquely identify a network resource in the whole internet.
    # From https://www.omnisecu.com/tcpip/what-are-port-and-socket-numbers.php
    while True:
        # Use accept to get the address of the sub-socket and the client
        client_socket, address = tcp_socket.accept()
        print(f"connection from {address}has been established")
        # send message
        client_socket.send(bytes(f"Welcome to this server!", "utf-8"))
        Others.recv_file(client_socket, 'my.txt')
        client_socket.close()
    

    服务器

    # client.py
    import socket
    import sys
    import Others
    #def main():
    # Create socket
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ip =sys.argv[1]
    # The port for my laptop
    port =int(sys.argv[2])
    # connect to the server
    tcp_socket.connect((ip, port))
    msg = tcp_socket.recv(1024)
    print(msg.decode('utf-8'))
    filename = input("please type in your file name \n")
    tcp_socket.send(bytes(filename,'utf-8'))
    Others.send_file(tcp_socket,filename)
    tcp_socket.close()
    print('connection closed')
    
    
    

    Others.py

    # Others.py
    def send_file(socket , filename):
        name = filename
        socket.send(bytes(f"{name}", 'utf-8'))
        #socket.send(codes)
        # read in binary mode
        file = open(filename, 'rb').read()
        socket.send(file)
     
    def recv_file(socket, filename):
        name = filename
        # write in binary mode
        file = open(name, 'wb')
        s = socket.recv(1024)
        while s:
            file.write(s)
            s = socket.recv(1024)
        file.close()
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月6日
  • 已采纳回答 10月29日
  • 创建了问题 10月29日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效