import socket, logging
from struct import *
from random import random
from select import select
from time import sleep
FORMAT = "%(asctime)-15s %(message)s"
logging.basicConfig(format = FORMAT)
logger = logging.getLogger('piztor_server')
logger.setLevel(logging.INFO)
def get_hex(data):
return "".join([hex(ord(c))[2:].zfill(2) for c in data])
class _SectionSize:
LENGTH = 4
OPT_ID = 1
STATUS = 1
USER_ID = 4
USER_TOKEN = 32
GROUP_ID = 4
ENTRY_CNT = 4
LATITUDE = 8
LONGITUDE = 8
LOCATION_ENTRY = USER_ID + LATITUDE + LONGITUDE
PADDING = 1
host = "69.85.86.42" #"localhost"
port = 2223
def gen_auth(username, password):
length = _SectionSize.LENGTH + \
_SectionSize.OPT_ID + \
len(username) + \
_SectionSize.PADDING + \
len(password) + \
_SectionSize.PADDING
data = pack("!LB", length, 0x00)
data += username
data += "\0"
data += password
data += "\0"
return data
def gen_auth_head_length(token, username):
return _SectionSize.USER_TOKEN + \
len(username) + \
_SectionSize.PADDING
def gen_update_location(token, username, lat, lng):
length = _SectionSize.LENGTH + \
_SectionSize.OPT_ID + \
gen_auth_head_length(token, username) + \
_SectionSize.LATITUDE + \
_SectionSize.LONGITUDE
data = pack("!LB32s", length, 0x01, token)
data += username
data += chr(0)
data += pack("!dd", lat, lng)
return data
def gen_request_location(token, username, gid):
length = _SectionSize.LENGTH + \
_SectionSize.OPT_ID + \
gen_auth_head_length(token, username) + \
_SectionSize.GROUP_ID
data = pack("!LB32s", length, 0x02, token)
data += username
data += chr(0)
data += pack("!L", gid)
return data
def gen_request_user_info(token, username, uid):
length = _SectionSize.LENGTH + \
_SectionSize.OPT_ID + \
gen_auth_head_length(token, username) + \
_SectionSize.USER_ID
data = pack("!LB32s", length, 0x03, token)
data += username
data += chr(0)
data += pack("!L", uid)
return data
def gen_logout(token, username):
length = _SectionSize.LENGTH + \
_SectionSize.OPT_ID + \
gen_auth_head_length(token, username)
data = pack("!LB32s", length, 0x04, token)
data += username
data += chr(0)
return data
def send(data):
received = bytes()
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
sock.sendall(data)
while True:
rd, wr, err = select([sock], [], [], 10)