Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python': 0.09; 'subject:howto': 0.09; "skip:' 30": 0.15; '16))': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'hint': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'pointers.': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:Convert': 0.16; 'subject:MAC': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'skip:" 40': 0.20; 'all,': 0.21; 'trying': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:( 20': 0.28; 'received:192.168.1.3': 0.29; 'convert': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'received:84': 0.32; "skip:' 20": 0.32; 'skip:s 30': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'but': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'leading': 0.61; 'skip:w 30': 0.61; 'dear': 0.66; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'construction': 0.72; 'reply- to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=YaM/Fntf c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=AAvI7MrX_rgA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=710N9AzS3JMA:10 a=shQ8fmVAfo_ONkPaBDIA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sun, 07 Oct 2012 21:04:38 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Convert MAC to hex howto References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349640280 news.xs4all.nl 6930 [2001:888:2000:d::a6]:43149 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30936 On 2012-10-07 20:44, Johannes Graumann wrote: > Dear all, > > I'm trying to convert > '00:08:9b:ce:f5:d4' > to > '\x00\x08\x9b\xce\xf5\xd4' > for use in magic packet construction for WakeOnLan like so: > wolsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) > wolsocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) > wolsocket.sendto('\xff'*6 + '\x00\x08\x9b\xce\xf5\xd4'*16, > (expectedlanbroadcast, 80)) > > This is what I came up whith, but I remain puzzled on how to preserver the > leading '\h' ... thanks for any hint > > expectedservermac = '00:08:9b:ce:f5:d4' > hexcall = str.split(expectedservermac,":") > hexcall.insert(0,'') > hexcall = "\\x".join(hexcall).decode('string_escape') > > Thanks for any pointers. > It looks like you're using Python 2, so: >>> s = '00:08:9b:ce:f5:d4' >>> "".join(chr(int(x, 16)) for x in s.split(":")) '\x00\x08\x9b\xce\xf5\xd4'