Cas*_*s22 3 html python string client
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")
Run Code Online (Sandbox Code Playgroud)
错误:
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
Run Code Online (Sandbox Code Playgroud)
不能用
urllib.request.urlopenurllib2.urlopenhttphttp.clienthttplib套接字只能接受字节,而您尝试向其发送Unicode字符串.
将字符串编码为字节:
s.send("GET /robots.txt HTTP/1.0\n\n".encode('ascii'))
Run Code Online (Sandbox Code Playgroud)
或者给它一个字节文字(以b前缀开头的字符串文字):
s.send(b"GET /robots.txt HTTP/1.0\n\n")
Run Code Online (Sandbox Code Playgroud)
考虑到您收到的数据也将是bytes值; 你不能只是比较那些''.只测试一个空响应,你可能想要解码str打印时的响应:
while True:
resp = s.recv(1024)
if not resp: break
print(resp.decode('ascii'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13051 次 |
| 最近记录: |