Skip to content

Instantly share code, notes, and snippets.

@leadscloud
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leadscloud/d30ca97619bdb3238f35 to your computer and use it in GitHub Desktop.
Save leadscloud/d30ca97619bdb3238f35 to your computer and use it in GitHub Desktop.
代码一,测试是没有任何问题的,但不支持多线程,单一请求。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Code is built by Ray. QQ: 75504026 E-mail:zhanglei@shibangcrusher.com
# Development environment: win7 64bit, python 2.7.8
import socket
import datetime
from time import sleep
import time
HOST = '192.168.1.170' # Symbolic name meaning the local host
PORT = 10049 # Port Set by DTU
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
sock.listen(5)
# connection, address = sock.accept()
def recv_timeout(the_socket,timeout=2):
#make socket non blocking
the_socket.setblocking(0)
#total data partwise in an array
total_data=[];
data='';
#beginning time
begin=time.time()
while 1:
#if you got some data, then break after timeout
if total_data and time.time()-begin > timeout:
break
#if you got no data at all, wait a little longer, twice the timeout
elif time.time()-begin > timeout*2:
break
#recv something
try:
data = the_socket.recv(8192)
if data:
total_data.append(data)
#change the beginning time for measurement
begin = time.time()
else:
#sleep for sometime to indicate a gap
time.sleep(0.1)
except:
pass
#join all parts to make final string
return ''.join(total_data)
try:
while True:
print "wating for connection..."
connection, address = sock.accept()
print "connected from", address
dtu_identification = ""
t_timeout = 60
t_begin_time = time.time()
while True:
# data = connection.recv(1024)
data = recv_timeout(connection)
if not len(data):
if time.time() - t_begin_time > t_timeout:
break
continue
'''
DTU-->DSC
1byte 1byte 2bytes 11bytes 4bytes 2bytes 1byte
0x7B 0x01 0x16 0x7B
'''
if data[0:4] == b'\x7b\x01\x00\x16' and data[21] == b'\x7b':
print "DTU is registering..."
dtu_identification = data[4:15]
m_ip = []
for x in data[15:19]:
m_ip.append(str(int(x.encode("hex"), 16)))
m_port = int(data[19:21].encode("hex"), 16)
print "DTU Login Num\tIP Address\tPort\tLogin Time"
print "%s\t%s\t%s\t%s" % (dtu_identification,
".".join(m_ip),
m_port,
datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
# DSC send confirm code to DTU
confirm_code = ("%s%s%s" % ('7b810010',
dtu_identification.encode("hex"), '7b'))
# sent_data = "7b81001031383536373737333533327b".decode("hex")
# confirm_code.decode("hex")
connection.send(confirm_code.decode("hex"))
t_begin_time = time.time()
elif data[0:2] == b'\x7b\x09':
# Recv buff data...
data_record_length = int(data[2:4].encode("hex"), 16)
start_p = 4 + len(dtu_identification)
end_p = start_p+(data_record_length-len(dtu_identification)-5)
data_record = data[start_p:end_p]
# print or process data in here
print ">>>DTU Say:", data_record
t_begin_time = time.time()
except BaseException, e:
print "Exception: %s" % repr(e)
sock.close()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment