I'm very new to development and may I know the equivalent python script for the following net cat command.
nc -v localhost 11211
#UPDATE
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('localhost', 11211))
data = s.recv(1024)
print(s.sendall(data.encode('get STATUS_MANAGER_KEYS')))
except:
print('error')
finally:
s.close()
Changed it as above but it showing an infinite execution at line data = s.recv(1024)
. May I know how to solve this.
You want to look into socket programming. Here's an example I found online https://steelkiwi.com/blog/working-tcp-sockets/
As you can see all netcat does is create a tcp connection to the network address and the port. It binds to it.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 11211))
s.listen(1)
conn, addr = s.accept()
while 1:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
conn.close()
You may also want to checkout this wikipedia on Network Sockets https://en.wikipedia.org/wiki/Network_socket
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With