How can I prevent unauthorized connections to my python socket server? -


i have simple server written in python opens , listens on tcp socket:

import socket  server_socket = socket.socket(socket.af_inet, socket.sock_stream) server_socket.bind(("", 4242)) server_socket.listen(5)  print("tcpserver waiting client on port 4242")  while 1:     client_socket, address = server_socket.accept()     print("i got connection ", address)     while 1:         data = client_socket.recv(512)         if data:             print(data)         if not data:             print("closed connection")             break 

and works great!

i left wondering... rouge client can connect server if know ip , port. given ip , port aren't crazy secure... how can make sure device can connect tcp socket own device?

can make sort of key? ssl about, having trouble understanding that. seems ssl protects data being intercepted, can still connect , write listener?

thanks!

you don't need ssl here. tsl , ssl protocols provide encrypted communication on network, if 'listening' communication, can't understand it.

the accept() method returns tuple (conn, address) (https://docs.python.org/2/library/socket.html)

you choose drop connection (don't recive data) if address doesn't match ip address.

if address == your_ip     # receive data 

if don't want programmatically, under linux, use packet filtering tool iptables.

$ iptables -i input -p tcp --destination-port 4242 -s <range_of_blocked_ip_addresses> -j drop 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -