Python 3 TypeError:「str」はバッファインターフェースをサポートしていません
Python 2ソケットの例を確認する
whois.py
import sys import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("whois.arin.net", 43)) s.send(sys.argv[1] + "\r\n") #Python 2.7 send signature #socket.send(string[, flags])
Python 3でコンパイルすると、次のエラーが表示されますか?
Traceback (most recent call last): File "C:\repos\hc\whois\python\whois.py", line 6, ins.send(sys.argv[1] + "\r\n") TypeError: 'str' does not support the buffer interface
溶液
Python 3では、ソケットはバイトを受け入れます。次のようなencode()
関数を使用して文字列をバイトに変換する必要があります。
whois.py
import sys import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("whois.arin.net", 43)) #convert string to bytes s.send((sys.argv[1] + "\r\n").encode()) #Python 3.4 send signature #socket.send(bytes[, flags])
P.S Tested with Python 3.4.3