I wrote a code to open a socket and then accept the socket and connect it. What is the problem? I want to filter the incoming connection ip. So I wrote a "if" statement for the connecting ip but it wont work. Looking forward for your help!
#!/usr/bin/python
import socket
s = socket.socket() # Create a socket
host = socket.gethostname()
port = 12345 # opening a port
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
c, addr = s.accept() # accept the client
c.send('waiting for connection...')
if '192' in addr: #---->This is what does not work.
print 'Got connection from', addr
c.send('Thank you for connecting')
print 'accepted'
else:
c.close()
print 'blocked.'
print '{0} tried to connect'.format(addr)
print 'a connection was request from', addr
raw_input("Press enter to continue: ")
addris a string containing the IP? maybe it is a tuple that contains the couple(address,port)