I want a normal format for the MAC-Address I got from get_node().
The format I get is 0x0L0xdL0x60L0x76L0x31L0xd6L, I want the 0x deleted and the L-term to a real hex number. It should be 00-0D-60-76-31-D6 .
How can I realize this?
def getNetworkData (self):
myHostname, myIP, myMAC = AU.getHostname()
touple1 = (myMAC & 0xFF0000000000) >> 40
touple2 = (myMAC & 0x00FF00000000) >> 32
touple3 = (myMAC & 0x0000FF000000) >> 24
touple4 = (myMAC & 0x000000FF0000) >> 16
touple5 = (myMAC & 0x00000000FF00) >> 8
touple6 = (myMAC & 0x0000000000FF) >> 0
readableMACadress = hex(touple1) + hex(touple2) + hex(touple3) + hex(touple4) + hex(touple5) + hex(touple6)
print readableMACadress
return myHostname, myIP, readableMACadress
Use
readableMACaddress = '%02X-%02X-%02X-%02X-%02X-%02X' % (touple1, touple2, touple3, touple4, touple5, touple6)
More concisely, you can eliminate the temporary touple variables by using
readableMACaddress = '-'.join('%02X' % ((myMAC >> 8*i) & 0xff) for i in reversed(xrange(6)))
Related
Firstly this questions is inferred from this answer answer .In that answer provides us a splitting a mp3 file with Python . That code is usefull , but for splitting two pieces . For instance if I want to split 30.00 th second to end of the audio , it is cool , but if I want to split from 30.00 to 35.00 it is useless for it . Below of that answer there is a comment about how to trim audio, like I said ,specific piece. When I imply that instructions to code it looked like that :
import struct
import sys
#MP3 frames are not independent because of the byte reservoir. This script does not account for
#that in determining where to do the split.
def SplitMp3(fi, firstSplit_sec,secondSplit_sec, out):
#Constants for MP3
bitrates = {0x0: "free", 0x1: 32, 0x2: 40, 0x3: 48, 0x4: 56, 0x5: 64, 0x6: 80, 0x7: 96, 0x8: 112,
0x9: 128, 0xa: 160, 0xb: 192, 0xc: 224, 0xd: 256, 0xe: 320, 0xf: "bad"}
freqrates = {0x0: 44100, 0x1: 48000, 0x2: 32000, 0x3: "reserved"}
countMpegFrames = 0
frameDuration = 0.026
unrecognizedBytes = 0
firstSplitFrame = int(round(firstSplit_sec / frameDuration))
secondSplitFrame = int(round(secondSplit_sec / frameDuration))
while True:
startPos = fi.tell()
#Check for 3 byte headers
id3Start = fi.read(3)
if len(id3Start) == 3:
if id3Start == b'TAG':
#print ("Found ID3 v1/1.1 header")
fi.seek(startPos + 256)
continue
if id3Start == b'ID3':
#Possibly a ID3v2 header
majorVer, minorVer, flags, encSize = struct.unpack(">BBBI", fi.read(7))
if majorVer != 0xFF and minorVer != 0xFF:
encSize1 = (encSize & 0x7f000000) >> 24
encSize2 = (encSize & 0x7f0000) >> 16
encSize3 = (encSize & 0x7f00) >> 8
encSize4 = (encSize & 0x7f)
if encSize1 < 0x80 and encSize2 < 0x80 and encSize3 < 0x80 and encSize4 < 0x80:
size = ((encSize & 0x7f000000) >> 3) + ((encSize & 0x7f0000) >> 2) + ((encSize & 0x7f00) >> 1) + (encSize & 0x7f)
unsync = (flags >> 7) & 0x1
extendedHeader = (flags >> 6) & 0x1
experimental = (flags >> 5) & 0x1
#print ("Found ID3v2 header")
#print ("version", majorVer, minorVer, unsync, extendedHeader, experimental)
#print ("size", size)
#TODO extendedHeader not supported yet
fi.seek(startPos + 10 + size)
continue
#Check for 4 byte headers
fi.seek(startPos)
headerRaw = fi.read(4)
if len(headerRaw) == 4:
headerWord = struct.unpack(">I", headerRaw)[0]
#Check for MPEG-1 audio frame
if headerWord & 0xfff00000 == 0xfff00000:
#print ("Possible MPEG-1 audio header", hex(headerWord))
countMpegFrames += 1
ver = (headerWord & 0xf0000) >> 16
bitrateEnc = (headerWord & 0xf000) >> 12
freqEnc = (headerWord & 0xf00) >> 8
mode = (headerWord & 0xf0) >> 4
cpy = (headerWord & 0xf)
if ver & 0xe == 0xa and freqEnc != 0xf:
#print ("Probably an MP3 frame")
bitrate = bitrates[bitrateEnc]
freq = freqrates[freqEnc >> 2]
padding = ((freqEnc >> 1) & 0x1) == 1
#print ("bitrate", bitrate, "kbps")
#print ("freq", freq, "Hz")
#print ("padding", padding)
frameLen = int((144 * bitrate * 1000 / freq ) + padding)
#Copy frame to output
fi.seek(startPos)
frameData = fi.read(frameLen)
if (secondSplitFrame >= countMpegFrames) and (countMpegFrames >= firstSplitFrame):
out.write(frameData)
fi.seek(startPos + frameLen)
continue
else:
raise RuntimeError("Unsupported format:", hex(ver), "header:", hex(headerWord))
#If no header can be detected, move on to the next byte
fi.seek(startPos)
nextByteRaw = fi.read(1)
if len(nextByteRaw) == 0:
break #End of file
unrecognizedBytes += 1
#print ("unrecognizedBytes", unrecognizedBytes)
#print ("countMpegFrames", countMpegFrames)
#print ("duration", countMpegFrames * frameDuration, "sec")
When I use this function it produces loosy output . For instance if I want to split 0.0 to 41.00 it gives me 0.00 to 37.00 , and this loosieness is increasing with amount of slice. I've struggled to understand some parts of code . So I am asking how can I solve that loosiness ? Am I missing something ?
Note : I already tried to pydub and similar modules . But they are useless . Always giving memory error and slow . This is really fast .
I couldn't find a solution for loosiness problem therefore I used ffmpeg for slicing .
subprocess.call(["ffmpeg","-i",input_file,"-acodec","copy","-loglevel","quiet","-ss",str(start),"-to",str(end),"-metadata","title={}".format(name),output_file+".mp3"])
Whenever I send a SYN packet using my program, I get no reply.I know the server is working because I can connect to it using the normal socket connect() method but when I try using RAW sockets to do it I get no reply, not even an RST.
Here is my packet according to Wireshark
Transmission Control Protocol, Src Port: 5173 (5173), Dst Port: 5005 n (5005), Seq: 0, Len: 0
Source Port: 5173
Destination Port: 5005
[Stream index: 15]
[TCP Segment Len: 0]
Sequence number: 0 (relative sequence number)
Acknowledgment number: 0
Header Length: 40 bytes
Flags: 0x002 (SYN)
000. .... .... = Reserved: Not set
...0 .... .... = Nonce: Not set
.... 0... .... = Congestion Window Reduced (CWR): Not set
.... .0.. .... = ECN-Echo: Not set
.... ..0. .... = Urgent: Not set
.... ...0 .... = Acknowledgment: Not set
.... .... 0... = Push: Not set
.... .... .0.. = Reset: Not set
.... .... ..1. = Syn: Set
.... .... ...0 = Fin: Not set
[TCP Flags: **********S*]
Window size value: 53270
[Calculated window size: 53270]
Checksum: 0x9f18 [incorrect, should be 0x90ae (maybe caused by "TCP checksum offload"?)]
Urgent pointer: 0
Options: (20 bytes), Maximum segment size, SACK permitted, Timestamps, No-Operation (NOP), Window scale
Maximum segment size: 65495 bytes
Kind: Maximum Segment Size (2)
Length: 4
MSS Value: 65495
TCP SACK Permitted Option: True
Kind: SACK Permitted (4)
Length: 2
Timestamps: TSval 378701, TSecr 0
Kind: Time Stamp Option (8)
Length: 10
Timestamp value: 378701
Timestamp echo reply: 0
No-Operation (NOP)
Type: 1
0... .... = Copy on fragmentation: No
.00. .... = Class: Control (0)
...0 0001 = Number: No-Operation (NOP) (1)
Window scale: 7 (multiply by 128)
[SEQ/ACK analysis]
And here is my Python code
#!/usr/bin/python
import socket
from struct import *
import random
s = socket.socket()
host = "127.0.0.1"
destination = "127.0.0.1"
CLRF = '\r\n'
#socket.gethostname()
print destination
port = 5173
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.connect((host, 5005))
try:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
except socket.error , msg:
print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
ipSource = '192.168.0.106'
#IP header
ipIHL = 5 # Header Length
ipVersion = 4 # ipv4/v6
ipTOS = 0 # type of service
ipTotalLen = 0 ## Kernel will fill correct length apparently
ipPackID = random.randint(1,1000)
#Flags
ipReserved = 0
ipNoFrag = 1
ipMoreFrags = 0
ipFragOffset = 0 #Fragment offset
ipTTL = 64
ipProtocol = socket.IPPROTO_TCP
ipChecksum = 0 # Magic kernel filling in at work again
ipSource = socket.inet_aton (host)
ipDest = socket.inet_aton (destination)
#Packing IP flags
ipFlags = ipMoreFrags + (ipNoFrag << 1) + (ipReserved << 2)
ipFragOffset = (ipFlags << 13) + ipFragOffset
ipIHLVersion = (ipVersion << 4) + ipIHL
headerIP = pack('!BBHHHBBH4s4s',ipIHLVersion, ipTOS, ipTotalLen, ipPackID, ipFragOffset, ipTTL, ipProtocol, ipChecksum, ipSource, ipDest)
#Checksum function
def carry_around_add(a, b):
c = a + b
return (c & 0xffff) + (c >> 16)
def checksum(msg):
s = 0
for i in range(0, len(msg), 2):
w = ord(msg[i]) + (ord(msg[i+1]) << 8)
s = carry_around_add(s, w)
return ~s & 0xffff
#TCP Header
tcpSourcePort = port #Source Port
tcpDestPort = 5005 #Destination Port
tcpSeqNum = 0 #Packet sequence
tcpAckNum = 0 #Ackknowledge Number
tcpOffset = 10 #Size of tcp header 20 bytes
#tcpReserved = 0
#tcpECN = 0
#Control Flags
tcpURG = 0
tcpACK = 0
tcpPSH = 0
tcpRST = 0
tcpSYN = 1
tcpFIN = 0
tcpWindow = socket.htons (5840) #Dunno how this works
tcpChecksum = 0
tcpUrgentPointer = 0
#TCP Options
tcpMaxSegmentSize = (2 << 24) + (4 << 16) + 65495 # Kind + Length + Max Segment Size
tcpSACKPermitted = (4 << 8) + 2#Kind + Length
#Split TCP TImestamps into 2 because too large
tcpTimestampPartOne = (8 << 8) + (10) #Kind + Length
tcpTimestampPartTwo = (378701 << 32) + 0 #Timestamp Value + Timestamp echo reply
tcpNoOp = (0 << 7) + (0 << 5) + 1 #Copy on fragmentation + Class + Number
tcpWindowScale = (3 << 16)+ (3 << 8) + 7 #Kind + Length(Bytes) +Shift CountS
#Combine both due to length issues
tcpNoOpAndWindowScale = (tcpNoOp << 24) + tcpWindowScale
tcpOffsetResult = (tcpOffset << 4) + 0 #Shift 4 bytes to left
#Putting together all the TCP Control Flags
tcpFlags = tcpFIN + (tcpSYN << 1) + (tcpRST << 2) + (tcpPSH << 3) + (tcpACK << 4) + (tcpURG << 5)
#Packing the pseudo TCP header
headerTCP = pack('!HHLLBBHHHLHHQL', tcpSourcePort, tcpDestPort, tcpSeqNum, tcpAckNum, tcpOffsetResult, tcpFlags, tcpWindow, tcpChecksum, tcpUrgentPointer, tcpMaxSegmentSize, tcpSACKPermitted, tcpTimestampPartOne, tcpTimestampPartTwo, tcpNoOpAndWindowScale)
#headerTCP = pack('!HHLLBBHHH', tcpSourcePort, tcpDestPort, tcpSeqNum, tcpAckNum, tcpOffsetResult, tcpFlags, tcpWindow, tcpChecksum, tcpUrgentPointer)
#data = 'GET ./asd HTTP/1.1'
data = ''
#Checksum Calculation
#Pseudo Header Fields
sourceAddr = socket.inet_aton(host)
destAddr = socket.inet_aton(destination)
placeholder = 0
protocol = socket.IPPROTO_TCP
tcpLen = len(headerTCP) + len(data)
psh = pack('!4s4sBBH', sourceAddr, destAddr, placeholder, protocol, tcpLen);
psh = psh + headerTCP + data;
#Calc checksum
tcpChecksumReal = (checksum(psh) << 1)
print(tcpChecksumReal)
#Pack actual tcp header with checksum
headerTCP = pack('!HHLLBBH', tcpSourcePort, tcpDestPort, tcpSeqNum, tcpAckNum, tcpOffsetResult, tcpFlags, tcpWindow) + pack('!H', 40728) + pack ('!H', tcpUrgentPointer) + pack('!LHHQL', tcpMaxSegmentSize, tcpSACKPermitted, tcpTimestampPartOne, tcpTimestampPartTwo, tcpNoOpAndWindowScale)
#Build full packet / ip with tcp with data
packet = headerIP + headerTCP + data
#print [hex(ord(c)) for c in packet]
s.sendto(packet, (destination,0))
Any help would be appreciated, thanks in advance.
Credits to #KenCheung for the answer
Turns out it was the checksum, the checksum from the headers I used as reference also were incorrect but the network card was offloading them.
I've been working on a small weather station for APRS using Direwolf and some python scripts on my RPi 3.
My dilemma seems simple, but I'm lacking in python knowledge.
I have my main code for my BMP180 sensor that outputs data in a specific format for the temp and pressure. However, I added a humidity sensor but I've been unable to combine the code to get the outputted format that I need.
Here's my main code with the code at the bottom that I need added in to get the desired output (also shown at the bottom for the print job)
i.e.
#050501z000/000g000t042r000p000P000h42b9999 RPI
Where
h42=h+humidity percentage.
#Main BMP180 code
-----------------------------------------------------------------------------
import smbus
import time
import sys
from ctypes import c_short
DEVICE = 0x77 # Default device I2C address
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
def convertToString(data):
# Simple function to convert binary data into
# a string
return str((data[1] + (256 * data[0])) / 1.2)
def getShort(data, index):
# return two bytes from data as a signed 16-bit value
return c_short((data[index] << 8) + data[index + 1]).value
def getUshort(data, index):
# return two bytes from data as an unsigned 16-bit value
return (data[index] << 8) + data[index + 1]
def readBmp180Id(addr=DEVICE):
# Chip ID Register Address
REG_ID = 0xD0
(chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)
return (chip_id, chip_version)
def readBmp180(addr=DEVICE):
# Register Addresses
REG_CALIB = 0xAA
REG_MEAS = 0xF4
REG_MSB = 0xF6
REG_LSB = 0xF7
# Control Register Address
CRV_TEMP = 0x2E
CRV_PRES = 0x34
# Oversample setting
OVERSAMPLE = 3 # 0 - 3
# Read calibration data
# Read calibration data from EEPROM
cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
# Convert byte data to word values
AC1 = getShort(cal, 0)
AC2 = getShort(cal, 2)
AC3 = getShort(cal, 4)
AC4 = getUshort(cal, 6)
AC5 = getUshort(cal, 8)
AC6 = getUshort(cal, 10)
B1 = getShort(cal, 12)
B2 = getShort(cal, 14)
MB = getShort(cal, 16)
MC = getShort(cal, 18)
MD = getShort(cal, 20)
# Read temperature
bus.write_byte_data(addr, REG_MEAS, CRV_TEMP)
time.sleep(0.005)
(msb, lsb) = bus.read_i2c_block_data(addr, REG_MSB, 2)
UT = (msb << 8) + lsb
# Read pressure
bus.write_byte_data(addr, REG_MEAS, CRV_PRES + (OVERSAMPLE << 6))
time.sleep(0.04)
(msb, lsb, xsb) = bus.read_i2c_block_data(addr, REG_MSB, 3)
UP = ((msb << 16) + (lsb << 8) + xsb) >> (8 - OVERSAMPLE)
# Refine temperature
X1 = ((UT - AC6) * AC5) >> 15
X2 = (MC << 11) / (X1 + MD)
B5 = X1 + X2
temperature = (B5 + 8) >> 4
# Refine pressure
B6 = B5 - 4000
B62 = B6 * B6 >> 12
X1 = (B2 * B62) >> 11
X2 = AC2 * B6 >> 11
X3 = X1 + X2
B3 = (((AC1 * 4 + X3) << OVERSAMPLE) + 2) >> 2
X1 = AC3 * B6 >> 13
X2 = (B1 * B62) >> 16
X3 = ((X1 + X2) + 2) >> 2
B4 = (AC4 * (X3 + 32768)) >> 15
B7 = (UP - B3) * (50000 >> OVERSAMPLE)
P = (B7 * 2) / B4
X1 = (P >> 8) * (P >> 8)
X1 = (X1 * 3038) >> 16
X2 = (-7357 * P) >> 16
pressure = P + ((X1 + X2 + 3791) >> 4)
return (temperature/10.0 * 9/5 +32,pressure/10)
def main():
(chip_id, chip_version) = readBmp180Id()
# print "Chip ID :", chip_id
# print "Version :", chip_version
(temperature,pressure)=readBmp180()
print time.strftime("#%d%H%Mz")+('000g000t0{0:0.0f}r000p000P000h00b{1:0.0f} RPI'.format(temperature, pressure))
if __name__=="__main__":
main()
#Code that needs to be added
import Adafruit_DHT
humidity = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
if humidity <= 99:
print('{0:0.0f}'.format(humidity))
else:
print('00')
#Desired output of print
#print time.strftime("#%d%H%Mz")+('000g000t0{0:0.0f}r000p000P000h00b{1:0.0f} RPI'.format(temperature, pressure))
#Where h00 = h(humidity output of the sensor)
Put import at the beginning of script and rest in main()
You can use {:02.0f} to get always two digits - ie. h09
import Adafruit_DHT
def main():
chip_id, chip_version = readBmp180Id()
temperature, pressure = readBmp180()
humidity = Adafruit_DHT.read_retry(Adafruit_DHT.AM2302, 17)
if humidity > 99:
huminidy = 0
print time.strftime("#%d%H%Mz") + '000g000t0{:0.0f}r000p000P000h{:02.0f}b{:0.0f} RPI'.format(temperature, huminidy, pressure)
if __name__=="__main__":
main()
Want to get equivalent python conversion for the following java code.
msg = "any text"
int len = msg == null ? 0 : msg.length ();
if (len > 0) {
byte lb[] = new byte[4];
lb[3] = (byte) (len & 0xFF);
lb[2] = (byte) ((len >> 8) & 0xFF);
lb[1] = (byte) ((len >> 16) & 0xFF);
lb[0] = (byte) ((len >> 24) & 0xFF);
}
Please help.
msg = "any text"
len = len(msg)
lb = []
if len > 0 :
lb.append(len & 0xFF)
lb.append((len >> 8) & 0xFF)
lb.append((len >> 16) & 0xFF)
lb.append((len >> 24) & 0xFF)
for num in reversed(lb):
print (num)
msg = "any text"
length = 0 if msg is None else len(msg)
if length > 0:
lb = []
lb.append((length >> 24) & 0xFF)
lb.append((length >> 16) & 0xFF)
lb.append((length >> 8) & 0xFF)
lb.append((length ) & 0xFF)
newmsg = b''.join(chr(i) for i in lb)
How to send long message using smpp.twisted python library.
I know how to send short message before 160 bytes.
For example:
ESME_NUM = '9090'
phone = '123456'
short_message = 'There is not short message.'*15
submit_pdu = SubmitSM(
source_addr=ESME_NUM,
destination_addr=phone,
short_message=short_message,
source_addr_ton=SOURCE_ADDR_TON,
dest_addr_ton=DEST_ADDR_TON,
dest_addr_npi=DEST_ADDR_NPI,
esm_class=EsmClass(EsmClassMode.DEFAULT, EsmClassType.DEFAULT),
protocol_id=0,
registered_delivery=RegisteredDelivery(
RegisteredDeliveryReceipt.SMSC_DELIVERY_RECEIPT_REQUESTED),
replace_if_present_flag=ReplaceIfPresentFlag.DO_NOT_REPLACE,
data_coding=DataCoding(DataCodingScheme.DEFAULT, DataCodingDefault.UCS2),
)
submitSMDeferred = smpp.sendDataRequest(submit_pdu)
If short_message > 160 b I cannot send message.
I found solution with using message_payload, but sms split on parts.
length = len(short_message)
splitat = 160
parts = length/splitat +1
submit_pdu = None
submitSMDeferred = defer.Deferred()
if length > splitat:
for k in range(parts):
msgpart = short_message[k*splitat:k*splitat+splitat]
self.logger.info('%s - %s - %s' % (msgpart, parts, k))
submit_pdu = SubmitSM(
source_addr=self.ESME_NUM,
destination_addr=source_addr,
source_addr_ton=self.SOURCE_ADDR_TON,
dest_addr_ton=self.DEST_ADDR_TON,
dest_addr_npi=self.DEST_ADDR_NPI,
esm_class=EsmClass(EsmClassMode.DEFAULT, EsmClassType.DEFAULT),
protocol_id=0,
registered_delivery=RegisteredDelivery(
RegisteredDeliveryReceipt.SMSC_DELIVERY_RECEIPT_REQUESTED),
replace_if_present_flag=ReplaceIfPresentFlag.DO_NOT_REPLACE,
data_coding=DataCoding(DataCodingScheme.DEFAULT, DataCodingDefault.UCS2),
sar_msg_ref_num = 1,
sar_total_segments = parts,
sar_segment_seqnum = k+1,
message_payload=msgpart
)
submitSMDeferred = smpp.sendDataRequest(submit_pdu)
I'm sorry. You just can use message_payload instead short_message.
Example:
ESME_NUM = '9090'
phone = '123456'
short_message = 'There is not short message.'*15
submit_pdu = SubmitSM(
source_addr=ESME_NUM,
destination_addr=phone,
message_payload=short_message,
source_addr_ton=SOURCE_ADDR_TON,
dest_addr_ton=DEST_ADDR_TON,
dest_addr_npi=DEST_ADDR_NPI,
esm_class=EsmClass(EsmClassMode.DEFAULT, EsmClassType.DEFAULT),
protocol_id=0,
registered_delivery=RegisteredDelivery(
RegisteredDeliveryReceipt.SMSC_DELIVERY_RECEIPT_REQUESTED),
replace_if_present_flag=ReplaceIfPresentFlag.DO_NOT_REPLACE,
data_coding=DataCoding(DataCodingScheme.DEFAULT, DataCodingDefault.UCS2),
)
smpp.sendDataRequest(submit_pdu).addBoth(message_sent)
Stay it here.