String Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • diffuser78@gmail.com

    #1

    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\x e5\x55\x03' is the MAC address to be used.


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

    str01=mac_strin g[0:2]
    str02=mac_strin g[2:4]
    str03=mac_strin g[4:6]
    str04=mac_strin g[6:8]
    str05=mac_strin g[8:10]
    str06=mac_strin g[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

  • Fredrik Lundh

    #2
    Re: String Question

    diffuser78@gmai l.com wrote:
    [color=blue]
    > 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\x e5\x55\x03' is the MAC address to be used.[/color]

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

    </F>



    Comment

    • Iain King

      #3
      Re: String Question


      diffuser78@gmai l.com wrote:[color=blue]
      > 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\x e5\x55\x03' is the MAC address to be used.
      >
      >
      > What I do is break the string into 6 parts like this,
      >
      > str01=mac_strin g[0:2]
      > str02=mac_strin g[2:4]
      > str03=mac_strin g[4:6]
      > str04=mac_strin g[6:8]
      > str05=mac_strin g[8:10]
      > str06=mac_strin g[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[/color]

      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

      Comment

      • diffuser78@gmail.com

        #4
        Re: String Question

        Many Thanks!! It worked like a charm.

        Fredrik Lundh wrote:[color=blue]
        > diffuser78@gmai l.com wrote:
        >[color=green]
        > > 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\x e5\x55\x03' is the MAC address to be used.[/color]
        >
        > s.sendto('\xff' *6 + binascii.unhexl ify(mac_string) *16, ('192.168.1.255 ', 80))
        >
        > </F>[/color]

        Comment

        • diffuser78@gmail.com

          #5
          Re: String Question

          I will try this one too...thanks for your response.
          Iain King wrote:[color=blue]
          > diffuser78@gmai l.com wrote:[color=green]
          > > 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\x e5\x55\x03' is the MAC address to be used.
          > >
          > >
          > > What I do is break the string into 6 parts like this,
          > >
          > > str01=mac_strin g[0:2]
          > > str02=mac_strin g[2:4]
          > > str03=mac_strin g[4:6]
          > > str04=mac_strin g[6:8]
          > > str05=mac_strin g[8:10]
          > > str06=mac_strin g[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[/color]
          >
          > 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[/color]

          Comment

          • Martin v. Löwis

            #6
            Re: String Question

            diffuser78@gmai l.com wrote:[color=blue]
            > 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[/color]

            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_he x("001485e55503 ")
            '\x00\x14\x85\x e5U\x03'

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

            Regards,
            Martin

            Comment

            • Tim Roberts

              #7
              Re: String Question

              "Iain King" <iainking@gmail .com> wrote:[color=blue]
              >
              >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))[/color]

              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@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              • Iain King

                #8
                Re: String Question


                Tim Roberts wrote:[color=blue]
                > "Iain King" <iainking@gmail .com> wrote:[color=green]
                > >
                > >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))[/color]
                >
                > 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@probo.com
                > Providenza & Boekelheide, Inc.[/color]

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

                Iain

                Comment

                Working...