2017-02-26 8 views
-1

私はこのプログラムの開発を持っているが、それは私には、タイトルに誤りが言うには、私は、UTF-8適用のために何かを入れて持っていると思うが、私は知らないどこかTypeErrorを解決するにはどうすればいいですか? 'str'ではなく、バイトのようなオブジェクトが必要ですか?

#!/usr/bin/python3 

import socket 

status = 0 

mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
mySocket.bind(('localhost', 1234)) 



mySocket.listen(5) 

while True: 
    print('Waiting for connections') 
    (recvSocket, address) = mySocket.accept() 
    print('HTTP request received:') 
    print(recvSocket.recv(1024)) 
    request = recvSocket.recv(1024) 

    slot = request.split(' ') 

    try: 
     num = int(slot[1][1:]) 
    except ValueError: 
     msg = ("Asegurese que su URL contiene un numero al final. Ejemplo: localhost:1234/56") 
     recvSocket.send("HTTP/1.1 200 OK\r\n\r\n" + "<html><body>" + msg + "</body></html>" + "\r\n") 
     status = 0 
+2

[TypeErrorの可能な複製: 'str'ではなくバイト様オブジェクトが必要です(http://stackoverflow.com/questions/35777639/typeerror-a-bytes-like-object-is-required-not -str) – Idos

答えて

1

sendbytesを期待する方法、stringではありません。 Python 3の文字列はUnicodeなので、文字列をバイトに変換する方法を指定する必要があります。文字列をbytesに変換するには、socket.send("some string".encode("utf-8"))のようなものを使用してください。

関連する問題