Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

String Question

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


diffuser78 at gmail

Jun 28, 2006, 8:35 AM

Post #1 of 13 (390 views)
Permalink
String Question

mac_string = '001485e55503' (This is the mac address of a computer.)

I am using wake on LAN python script to start computer remote.It uses
format like this ....

s.sendto('\xff'*6 + '\x00\x014\x85\xe5\x55\x03'*16, ('192.168.1.255',
80))

where '\x00\x14\x85\xe5\x55\x03' is the MAC address to be used.


What I do is break the string into 6 parts like this,

str01=mac_string[0:2]
str02=mac_string[2:4]
str03=mac_string[4:6]
str04=mac_string[6:8]
str05=mac_string[8:10]
str06=mac_string[10:12]

and if I use it like this

s.sendto('\xff'*6 + '\xstr01\xstr02\xstr03\xstr04\xstr05\xstr06'*16,
('192.168.1.255', 80))
I get an error


I also tried like this
s.sendto('\xff'*6 + 'mac_string'*16, ('192.168.1.255', 80))

Thiis also didnt work.


Since the MAC adddress are hexadecimal, how should I go about it here.

Please help, every help is appreciated. Thanks

--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Jun 28, 2006, 8:45 AM

Post #2 of 13 (379 views)
Permalink
Re: String Question [In reply to]

diffuser78[at]gmail.com wrote:

> mac_string = '001485e55503' (This is the mac address of a computer.)
>
> I am using wake on LAN python script to start computer remote.It uses
> format like this ....
>
> s.sendto('\xff'*6 + '\x00\x014\x85\xe5\x55\x03'*16, ('192.168.1.255',
> 80))
>
> where '\x00\x14\x85\xe5\x55\x03' is the MAC address to be used.

s.sendto('\xff'*6 + binascii.unhexlify(mac_string) *16, ('192.168.1.255', 80))

</F>



--
http://mail.python.org/mailman/listinfo/python-list


iainking at gmail

Jun 28, 2006, 8:48 AM

Post #3 of 13 (380 views)
Permalink
Re: String Question [In reply to]

diffuser78[at]gmail.com wrote:
> mac_string = '001485e55503' (This is the mac address of a computer.)
>
> I am using wake on LAN python script to start computer remote.It uses
> format like this ....
>
> s.sendto('\xff'*6 + '\x00\x014\x85\xe5\x55\x03'*16, ('192.168.1.255',
> 80))
>
> where '\x00\x14\x85\xe5\x55\x03' is the MAC address to be used.
>
>
> What I do is break the string into 6 parts like this,
>
> str01=mac_string[0:2]
> str02=mac_string[2:4]
> str03=mac_string[4:6]
> str04=mac_string[6:8]
> str05=mac_string[8:10]
> str06=mac_string[10:12]
>
> and if I use it like this
>
> s.sendto('\xff'*6 + '\xstr01\xstr02\xstr03\xstr04\xstr05\xstr06'*16,
> ('192.168.1.255', 80))
> I get an error
>
>
> I also tried like this
> s.sendto('\xff'*6 + 'mac_string'*16, ('192.168.1.255', 80))
>
> Thiis also didnt work.
>
>
> Since the MAC adddress are hexadecimal, how should I go about it here.
>
> Please help, every help is appreciated. Thanks

See http://docs.python.org/lib/typesseq-strings.html

You probably want:

s.sendto('\xff'*6 + ('\x%s\x%s\x%s\x%s\x%s\x%s' % (str01, str02, str03,
sttr04, str05, str06))*16, ('192.168.1.255', 80))

Iain

--
http://mail.python.org/mailman/listinfo/python-list


diffuser78 at gmail

Jun 28, 2006, 8:56 AM

Post #4 of 13 (381 views)
Permalink
Re: String Question [In reply to]

Many Thanks!! It worked like a charm.

Fredrik Lundh wrote:
> diffuser78[at]gmail.com wrote:
>
> > mac_string = '001485e55503' (This is the mac address of a computer.)
> >
> > I am using wake on LAN python script to start computer remote.It uses
> > format like this ....
> >
> > s.sendto('\xff'*6 + '\x00\x014\x85\xe5\x55\x03'*16, ('192.168.1.255',
> > 80))
> >
> > where '\x00\x14\x85\xe5\x55\x03' is the MAC address to be used.
>
> s.sendto('\xff'*6 + binascii.unhexlify(mac_string) *16, ('192.168.1.255', 80))
>
> </F>

--
http://mail.python.org/mailman/listinfo/python-list


diffuser78 at gmail

Jun 28, 2006, 8:59 AM

Post #5 of 13 (381 views)
Permalink
Re: String Question [In reply to]

I will try this one too...thanks for your response.
Iain King wrote:
> diffuser78[at]gmail.com wrote:
> > mac_string = '001485e55503' (This is the mac address of a computer.)
> >
> > I am using wake on LAN python script to start computer remote.It uses
> > format like this ....
> >
> > s.sendto('\xff'*6 + '\x00\x014\x85\xe5\x55\x03'*16, ('192.168.1.255',
> > 80))
> >
> > where '\x00\x14\x85\xe5\x55\x03' is the MAC address to be used.
> >
> >
> > What I do is break the string into 6 parts like this,
> >
> > str01=mac_string[0:2]
> > str02=mac_string[2:4]
> > str03=mac_string[4:6]
> > str04=mac_string[6:8]
> > str05=mac_string[8:10]
> > str06=mac_string[10:12]
> >
> > and if I use it like this
> >
> > s.sendto('\xff'*6 + '\xstr01\xstr02\xstr03\xstr04\xstr05\xstr06'*16,
> > ('192.168.1.255', 80))
> > I get an error
> >
> >
> > I also tried like this
> > s.sendto('\xff'*6 + 'mac_string'*16, ('192.168.1.255', 80))
> >
> > Thiis also didnt work.
> >
> >
> > Since the MAC adddress are hexadecimal, how should I go about it here.
> >
> > Please help, every help is appreciated. Thanks
>
> See http://docs.python.org/lib/typesseq-strings.html
>
> You probably want:
>
> s.sendto('\xff'*6 + ('\x%s\x%s\x%s\x%s\x%s\x%s' % (str01, str02, str03,
> sttr04, str05, str06))*16, ('192.168.1.255', 80))
>
> Iain

--
http://mail.python.org/mailman/listinfo/python-list


martin at v

Jun 28, 2006, 9:38 AM

Post #6 of 13 (381 views)
Permalink
Re: String Question [In reply to]

diffuser78[at]gmail.com wrote:
> mac_string = '001485e55503' (This is the mac address of a computer.)
> Since the MAC adddress are hexadecimal, how should I go about it here.
>
> Please help, every help is appreciated. Thanks

I could not quite understand what you are trying to achieve, but
it appears that you want to convert a character strings of hexadecimal
characters into a byte string with the same bytes. I recommend this:

py> import binascii
py> binascii.a2b_hex("001485e55503")
'\x00\x14\x85\xe5U\x03'

Of course, if the string is fixed, you could just as well use the
result string (i.e. '\x00\x14\x85\xe5U\x03') directly.

Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


timr at probo

Jun 29, 2006, 11:57 PM

Post #7 of 13 (380 views)
Permalink
Re: String Question [In reply to]

"Iain King" <iainking[at]gmail.com> wrote:
>
>You probably want:
>
>s.sendto('\xff'*6 + ('\x%s\x%s\x%s\x%s\x%s\x%s' % (str01, str02, str03,
> sttr04, str05, str06))*16, ('192.168.1.255', 80))

You probably should TRY suggestions before you post them. That will get an
"invalid \x escape". \x must be followed by exactly two hex digits. You
can't build up an escape sequence like this.
--
- Tim Roberts, timr[at]probo.com
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list


iainking at gmail

Jun 30, 2006, 1:09 AM

Post #8 of 13 (377 views)
Permalink
Re: String Question [In reply to]

Tim Roberts wrote:
> "Iain King" <iainking[at]gmail.com> wrote:
> >
> >You probably want:
> >
> >s.sendto('\xff'*6 + ('\x%s\x%s\x%s\x%s\x%s\x%s' % (str01, str02, str03,
> > sttr04, str05, str06))*16, ('192.168.1.255', 80))
>
> You probably should TRY suggestions before you post them. That will get an
> "invalid \x escape". \x must be followed by exactly two hex digits. You
> can't build up an escape sequence like this.
> --
> - Tim Roberts, timr[at]probo.com
> Providenza & Boekelheide, Inc.

You're right. I'd foolishly assumed that \x was just another character
code, like \t or \n. Apologies.

Iain

--
http://mail.python.org/mailman/listinfo/python-list


root at sys1

Jun 23, 2008, 7:54 AM

Post #9 of 13 (328 views)
Permalink
Re: String question [In reply to]

Wow...about ten seconds to get a kind response ....
Thanks Tim.

Andrew.

Tim Golden wrote:
> Andreu wrote:
>> I want to split a sentence and assign each word to a variable.
>> In Ruby I can do it as:
>>
>> v1,v2,v3,v4,v5 = str1.split
>>
>> Which will be the Python equivalent ? Thanks.
>
> That would be:
>
> str1 = "The quick brown fox jumps"
> v1, v2, v3, v4, v5 = str1.split ()
>
> TJG
--
http://mail.python.org/mailman/listinfo/python-list


root at sys1

Jun 23, 2008, 3:13 PM

Post #10 of 13 (317 views)
Permalink
Re: String question [In reply to]

Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
does not seem to work. My original problem was I forgot about
the parenthesis as Tim point out. So I ended up converting to a
list as in: v = str1.split() and accessing the elements using
v[0] v[1] ect...it is working now. Thanks.

Andreu.


cokofreedom[at]gmail.com wrote:
> However I would make a list of it.
>>>> v_list = example.split()
> That seems to me to be the more pythonic way to do it, since it is
> dynamic.
--
http://mail.python.org/mailman/listinfo/python-list


M8R-yfto6h at mailinator

Jun 23, 2008, 8:38 PM

Post #11 of 13 (313 views)
Permalink
Re: String question [In reply to]

"Andreu" <root[at]sys1.org> wrote in message news:g3p72i$432$1[at]aioe.org...
> Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
> does not seem to work. My original problem was I forgot about
> the parenthesis as Tim point out. So I ended up converting to a
> list as in: v = str1.split() and accessing the elements using
> v[0] v[1] ect...it is working now. Thanks.
>
> Andreu.

v1,v2,v3 = str1.split() will only work if there are exactly three things in
str1.

>>> s = 'this is a test'
>>> v1,v2,v3 = s.split()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: too many values to unpack
>>> v1,v2,v3 = s.split(' ',2) # limit to two splits maximum
>>> v1
'this'
>>> v2
'is'
>>> v3
'a test'

-Mark

--
http://mail.python.org/mailman/listinfo/python-list


cokofreedom at gmail

Jun 24, 2008, 12:53 AM

Post #12 of 13 (311 views)
Permalink
Re: String question [In reply to]

On Jun 24, 5:38 am, "Mark Tolonen" <M8R-yft...@mailinator.com> wrote:
> "Andreu" <r...@sys1.org> wrote in messagenews:g3p72i$432$1[at]aioe.org...
> > Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
> > does not seem to work. My original problem was I forgot about
> > the parenthesis as Tim point out. So I ended up converting to a
> > list as in: v = str1.split() and accessing the elements using
> > v[0] v[1] ect...it is working now. Thanks.
>
> > Andreu.
>
> v1,v2,v3 = str1.split() will only work if there are exactly three things in
> str1.
>
> >>> s = 'this is a test'
> >>> v1,v2,v3 = s.split()
>
> Traceback (most recent call last):
> File "<interactive input>", line 1, in <module>
> ValueError: too many values to unpack>>> v1,v2,v3 = s.split(' ',2) # limit to two splits maximum
> >>> v1
> 'this'
> >>> v2
> 'is'
> >>> v3
>
> 'a test'
>
> -Mark

In Python 3k I believe you can put a * next to one of the variables to
hold multiple arguments. That'll be aidful!
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Jun 24, 2008, 11:40 AM

Post #13 of 13 (310 views)
Permalink
Re: String question [In reply to]

cokofreedom[at]gmail.com wrote:
> On Jun 24, 5:38 am, "Mark Tolonen" <M8R-yft...@mailinator.com> wrote:


> In Python 3k I believe you can put a * next to one of the variables to
> hold multiple arguments. That'll be aidful!

IDLE 3.0b1
>>> a,b,*c=[1,2,3,4,5]
>>> c
[3, 4, 5]
>>> a,*b,c = [1,2,3,4,5]
>>> b
[2, 3, 4]
>>> a,b,*c=[1,2]
>>> c
[]

Augmented assignment is quite similar to argument passing:
at most one starred target;
at least as many items as un-starred targets (as now).

tjr

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.