您好我正在尝试在 python 3.3 中学习套接字,但在向远程服务器发送数据时遇到问题。是否在 python 3.3 中更改了命令,或者我只是做错了。请找到下面的代码,以便您查看问题。更喜欢简单的 splution,例如将 s.sendall 更改为 s.send 之类的。如果不可能,请不要担心
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print("Failed to create a socket")
print("Socket created")
host = ("www.google.com")
port = 80
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
print("Hostname could not be found, exiting finding socket")
sys.exit()
print("Ip adress of",host,"is",remote_ip)
s.connect((remote_ip, port))
print("Socket connected to",host,"on ip", remote_ip)
message = "GET / HTTP/1.1/r/n/r/n"
try:
s.send(message)
except socket.error:
print("Failed")
sys.exit
print("Message send successful")
reply = s.recv(4096)
print(reply)
s.close()