I can't understand why this code works fine,
echo as3333 | nc stat.ripe.net 43
but the equivalent code in Python returns nothing
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('stat.ripe.net', 43))
sock.send('as3333'.encode('utf-8'))
tmp = sock.recv(1024)
print(tmp.decode('utf-8')) #no bytes there
sock.close()
Thanks!
It is not exactly the same. You forgot the newline and sendall
. Fixed code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('stat.ripe.net', 43))
sock.sendall('as3333\r\n'.encode('utf-8'))
response = b''
while True:
tmp = sock.recv(65536)
if not tmp:
break
response += tmp
print(response.decode('utf-8'))
sock.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.