import socket
# Set up a TCP/IP socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Connect as client to a selected server
# on a specified port
s.connect(("www.wellho.net",80))
# Protocol exchange - sends and receives
s.send("GET /robots.txt HTTP/1.0\n\n")
while True:
resp = s.recv(1024)
if resp == "": break
print(resp,)
# Close the connection when completed
s.close()
print("\ndone")
Error:
cg0546wq@smaug:~/Desktop/440$ python3 HTTPclient.py
Traceback (most recent call last):
File "HTTPclient.py", line 11, in <module>
s.send("GET /robots.txt HTTP/1.0\n\n")
TypeError: 'str' does not support the buffer interface
不能用
urllib.request.urlopen
urllib2.urlopen
http
http.client
httplib
套接字只能接受bytes,而您尝试向其发送 Unicode 字符串。
将字符串编码为字节:
s.send("GET /robots.txt HTTP/1.0\n\n".encode('ascii'))
或者给它一个字节文字(以a开头的字符串文字)b
字首):
s.send(b"GET /robots.txt HTTP/1.0\n\n")
考虑到您的数据receive也将是bytes
价值观;你不能只将它们与''
。只需测试一个empty响应,并且您可能希望将响应解码为str
打印时:
while True:
resp = s.recv(1024)
if not resp: break
print(resp.decode('ascii'))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)