Skip to content

Instantly share code, notes, and snippets.

@edprince
Last active August 29, 2015 14:05
Show Gist options
  • Save edprince/6ce6f44c938b87018b00 to your computer and use it in GitHub Desktop.
Save edprince/6ce6f44c938b87018b00 to your computer and use it in GitHub Desktop.
Work project
function init() {
document.getElementById("Submit").addEventListener('click', ajax);
}
function ajax() {
console.log("Function started");
request = new XMLHttpRequest();
request.open('GET', 'getData.php', true);
request.onload = function() {
console.log(request);
if (request.status >= 200 && request.status < 400){
// Success!
document.getElementById("displayData").innerHTML = request.responseText;
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
console.log("Request sent");
}
document.addEventListener('load', init);
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="app.js"></script>
</head>
<!--Number of results form-->
<body>
<p class='title'>Presenting udp packet data</p><hr />
<center><div class='data-window'>
<form id="getSearchNumber">
<label for="imei">IMEI of desired devices</label>
<select name="imei" id="imei">
<option value="Alldata">All Data</option>
<option value="012981000298213">012981000298213</option>
</select>
<br />
<button id="Submit">Submit</button>
</form>
<div id="displayData"></div>
</body>
</html>
import socket
import sys
import MySQLdb
db = MySQLdb.connect('localhost', 'othello', 'simpleunit10', 'encodeddata')
HOST = "0.0.0.0"
PORT = 30000
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print "Socket created"
except socket.error as msg:
print 'Failed to create socket. Error Code : ' + str(msg[0] + ' Message ' + msg[1])
sys.exit(0)
try:
s.bind((HOST, PORT))
print "bind Successful"
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
while True:
try:
print "waiting for data..."
data, (ip, port) = s.recvfrom(1024)
# data = d[0]
# addr = d[1]
#d = s.recvfrom(1024)
#data = d[0]
#addr = d[1]
print "Received >", data
cur = db.cursor()
except:
print "Error recieving data"
try:
cur.execute("""INSERT INTO data (packetdata) VALUES (%s)""",data)
print "Successfully inserted data"
db.commit()
#Send Acknowledgement
UDP_IP = ip
UDP_PORT = port
Message = 0x01
s.sendto(Message, (UDP_IP, UDP_PORT))
print "Acknowledgement sent"
except:
print "Error inserting data into table"
print ip
print port
print data
db.close()
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# if not data:
# break
# reply = 'OK..' + data
# s.sendto(reply, addr)
# print 'message[' + addr[0] + ':' + str(addr[1]) + '] - ' + data.strip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment