所以这周我的课程是让我做一个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
服务器代码:
import struct
#Echo Server
import socket
import sys
#create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#Bind the socket to the port
server_address = ('localhost', 10000)
print(sys.stderr, 'starting up on %s port %s' % server_address)
sock.bind(server_address)
#Listen for incoming connections
sock.listen(1)
while True:
#Wait for a connection
print(sys.stderr, 'waiting for a connection')
connection, client_address = sock.accept()
try:
print(sys.stderr, 'connection from', client_address)
#Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print(sys.stderr, 'received "%s"' % data)
if data:
print(sys.stderr, 'sending data back to client')
connection.sendall(data)
else:
print(sys.stderr, 'no more data from', client_address)
break
finally:
#opening block.dd file
block_MBR = open('block.dd', 'rb')
#Going to address 1BE (hex), which is byte 446 according to the Wikipedia MBR page.
block_MBR.seek(446)
#Read 1 byte from the position 446 to return the status byte
status_byte = block_MBR.read(1)
#print the status byte
print("The status byte is", status_byte)
#Go to address 1BE +4 (hex), which is byte 450
block_MBR.seek(450)
#Reading 1 byte at the position 450 to find the partition type.
partition_type = block_MBR.read(1)
#print the partition type.
print("The partition type is", partition_type)
#Go to address 1BE + 8 (hex), which is byte 454.
block_MBR.seek(454)
#Read 4 bytes (or 32 bits) at position 454 to find the address of the first sector.
address_first_sector = struct.unpack('i',block_MBR.read(4))[0]
#print the address of the first sector in the partition.
print("The address of the first sector in the partition is", address_first_sector)
#Clean up the connection
connection.close()
服务器输出:
Traceback (most recent call last):
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> starting up on localhost port 10000
File "C:/Users/jesse/PycharmProjects/untitled/CYBR-260-40A/Week 4/KimmelFreeman_Jessica_CYBR260_40A_Week4ProgrammingAssignment.py", line 18, in <module>
sock.bind(server_address)
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
Process finished with exit code 1
答案 0 :(得分:1)
在客户端上,message = open('block.dd', 'rb')
是<class '_io.BufferedReader'>
而不是字节。因此,您需要读取文件message.read()
。我认为您需要this
客户:
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')
sdata = message.read()
print('sending "%s"' % sdata)
sock.sendall(sdata)
message.close()
#Look for the response
amount_expected = len(sdata)
while amount_expected:
data = sock.recv(16)
amount_expected -= len(data)
print('received "%s:' % data)
finally:
print(sys.stderr, "closing socket")
sock.close()