i wanna share my code using socket,by set esp8266 as access point this code can filter banned ip address,this code can treat as AT command protocol .
Please give comment ,
server.py:
#for ESP8266
Code: Select all
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_address = ("", 11000)
print("Starting up on =",server_address)
sock.bind(server_address)
sock.listen(1)
while True:
print ('waiting for a connection')
connection, client_address = sock.accept()
print (client_address[0])
# can use to filter ip address
if client_address[0] == '192.168.0.88':
print ("Client ip is banned")
else:
try:
print ('connection from', client_address)
while True:
data = connection.recv(1024)
print('received ' ,data)
if data:
print ('sending data back to the client')
datainput=input('>>')
datainput = bytes(datainput,"utf-8")
print (datainput)
connection.sendall(datainput)
else:
print ('no more data from', client_address)
break
finally:
connection.close()
# for pc
Code: Select all
import socket
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('192.168.4.1', 11000)
print ('connecting to %d port %d', server_address)
sock.connect(server_address)
try:
message ='This is the message. It will be repeated '
sock.sendall(message)
(data,addr) = sock.recvfrom(1024)
print(data)
finally:
print ('closing socket')
sock.close()
guy wish me luck for my exam.