Python socket server : Reject connection from address -
i have python socket server listens on port, , accepts incoming connections using:
(conn, address) = socket.accept()
however, wish accept connections ip address.
currently, close connection if address isn't registered, accomplish this.
but there better way this, directly rejecting connections unregistered addresses, instead of accepting connections , closing them?
it's not possible indicate connection refused clients ip addresses, , establish connection clients other ip addresses. not python limitation, lower-level, bsd socket layer limitation. can't c.
the closest behavior in general can in python closing connection after has been accepted:
sock, addr = server_socket.accept() if addr[0] != '12.34.56.78': sock.close() return ...
then client see connection being accepted, , shortly after client see eof when reading it, , wouldn't able write it.
however it's possible limit interface (i.e. network card) @ bind time, using 1 of:
server_socket.bind(('', 65432)) # bind on interface. server_socket.bind(('127.0.0.1', 65432)) # bind on loopback (localhost clients only). server_socket.bind(('34.56.78.91', 65432))
so in 127.0.0.1 version, telnet 127.0.0.1 65432
(as client) work, telnet myhostname 65432
yield connection refused (and server_socket.accept()
call won't connection).
Comments
Post a Comment