Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This python program is a web server. When i compile it, it has an error: TypeErr

ID: 649194 • Letter: T

Question

This python program is a web server. When i compile it, it has an error: TypeError:'str' does not support the buffer interface. How do I fix this? The code to which the error appears is in italicized bold.

from socket import *
def main():

#import socket module
#from socket import *
serverPort=8592
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a server socket
serverSocket.bind((' ',serverPort))
serverSocket.listen(1)
while True:
#Establish the connection
print ('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024)
print (message, '::'), message.split()[0], ':',message.split()[1]
filename = message.split()[1]
print (filename), '||', filename[1:]
f = open(filename[1:])
outputdata = f.read()

#Send one HTTP header line into socket
connectionSocket.send(' HTTP/1.1 200 0K ')
connectionSocket.send(outputdata)
connectionSocket.close()

#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i])
connectionSocket.close()

except IOError:
pass

#Send response message for file not found
print ("404 Not Found")
connectionSocket.send('HTTP/1.1 404 Not Found ')
#Close client socket
#Fill in start
#Fill in end

serverSocket.close()

if __name__ == '__main__':
main()

Explanation / Answer

import socket, struct def main(host): # Connect to server and get image size. client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((host, 65000)) packed = recvall(client, struct.calcsize('!I')) # Decode the size and get the image data. size = struct.unpack('!I', packed)[0] print('Receiving data from:', host) data = recvall(client, size) # Shutdown the socket and create the image file. client.shutdown(socket.SHUT_RDWR) client.close() with open('image.bmp', 'wb') as file: file.write(data) def recvall(sock, size): message = bytearray() # Loop until all expected data is received. while len(message)
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote