Python 3 问题:处理服务器/客户端问题

所以本周我的课程让我做一个 tcp 服务器/客户端,其中信息通过连接传递,然后由服务器解析。所以我已经为两者编写了代码,但我收到了我不明白的返回错误。我应该返回状态字节、分区类型和第一部分地址的信息。我不明白为什么我会遇到客户端的问题,当消息是二进制的时,这就是说我没有使用(我认为!),而且我认为我没有使用套接字服务器的多个位置。我该如何解决这个烂摊子?我有没有提到我只是在学习 Python?因为我是。


客户端代码:


import socket

import sys


#Create a TCP/IP Socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


#Connect the socket to the port where the server is listening

server_address = ('localhost', 10000)

print(sys.stderr, 'connecting to %s port %s' % server_address)

sock.connect(server_address)


try:


    #Send data

    #filename = 'block.dd'

    message = open('block.dd', 'rb')

    print(sys.stderr, 'sending "%s"' % message)

    message.close()

    sock.sendall(message)



    #Look for the response

    amount_received = 0

    amount_expected = len(message)


    while amount_received < amount_expected:

        data = sock.recv(16)

        amount_received += len(data)

        print(sys.stderr, 'received "%s:' % data)


finally:

    print(sys.stderr, "closing socket")

    sock.close()

客户端问题:


<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> connecting to localhost port 10000

<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> sending "<_io.BufferedReader name='block.dd'>"

Traceback (most recent call last):

<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> closing socket

  File "C:/Users/jesse/PycharmProjects/untitled/CYBR-260-40A/Week 4/KimmelFreeman_Jessica_CYBR260_40A_Week4ProgramminAssignment_Client.py", line 24, in <module>

    sock.sendall(message)

TypeError: a bytes-like object is required, not '_io.BufferedReader'


Process finished with exit code 1


侃侃无极
浏览 561回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python