[Tutor] a beginning question

Paul Z paul at whoosh.cn
Sat Feb 20 08:23:33 EST 2016


Hi,

I writed some codes as the UDP messages:

import socket 
import random
from array import *

port = 8088
host = "localhost"

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

num = array('f')
x = 0
while x < 6:
    num.append(random.random())
    x += 1

a0 = str("%.2f"%num[0]) + ','
a1 = str("%.2f"%num[1]) + ','
a2 = str("%.2f"%num[2]) + ','
a3 = str("%.2f"%num[3]) + ','
a4 = str("%.2f"%num[4]) + ','
a5 = str("%.2f"%num[5]) + ','

msg1 = 'a,' + a0 + a1 + a2
msg1 = bytes(msg1, 'utf-8')
msg2 = 'b,' + a3 + a4 + a5
msg2 = bytes(msg2, 'utf-8')

s.sendto(msg1, (host, port))
s.sendto(msg2, (host, port))

and I receive the messages via:

import socket 
port = 8088
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
s.bind(("",port)) 
print('waiting on port:',port)
while True: 
  data,addr = s.recvfrom(1024) 
  print('reciveed:', data, "from", addr)

I want to arrange the messages to:

array1 = #the numbers which is start as 'a' in the messages 
array2 = #the numbers which is start as b in the messages

Thanks!
Paul Z 

 

----------------------------------------
> From: paul at whoosh.cn
> To: alan.gauld at btinternet.com
> CC: tutor at python.org
> Subject: RE: [Tutor] a beginning question
> Date: Sat, 20 Feb 2016 19:51:16 +0800
>
> Hi Alan,
>
> Thanks for your reply,
>
> My friend help me to write a andriod app which send UDP messages.
> And I writed some codes by using Python to receive the messages as below:
>
> import socket
> port = 8081
> s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> s.bind(("",port))
> print('waiting on port:',port)
> while True:
>   data,addr = s.recvfrom(1024)
>   print('reciveed:', data, "from", addr)
>
> and I plan to re-pack them, and send them to an other computer and port.
> I think my code should judge the messages which is "a..." or "b..." first, then arrange them as below:
>
> a = (0.1, 0.6, 0.7)
> b = (0.2, 0.3, 0.8)
>
> the numbers in the example is variable.
> and The messages I recived from andriod app are some strings which is as the head 'a' or 'b', such as 'a, 0.1, 0.6, 0.7'.
>
> Thanks
> Paul Z
>
> ----------------------------------------
>> To: tutor at python.org
>> From: alan.gauld at btinternet.com
>> Date: Sat, 20 Feb 2016 10:33:51 +0000
>> Subject: Re: [Tutor] a beginning question
>>
>> On 20/02/16 09:15, Paul Z wrote:
>>> Hi all,
>>>
>>> I receive the string messages from my Mobile via UDP as below:
>>> A, 0.1, 0.6, 0.7
>>> B, 0.2, 0.3, 0.8
>>
>> I think we need a bit more detail.
>> How do you receive these messages? UDP requires some
>> kind of receiving service, so what is it that is
>> receiving these messages? Is it your python program?
>> Or is it some other bit of software?
>>
>> Having received these strings what happens?
>> Are they stored in a variable? In a file? where?
>>
>>> I want to arrange them to two array as below:
>>>
>>> a = (0.1, 0.6, 0.7)
>>> b = (0.2, 0.3, 0.8)
>>
>> What you've shown would be tuples in Python, not
>> arrays. We need to be precise about data types.
>> Do you want the data in a list, a tuple, an array?
>> Do you understand the differences?
>>
>> Now, to try to answer your question based on the
>> most optimistic set of assumptions:
>> ie. You are receiving the messages in your python code
>> and storing them as string variables called s1
>> and s2 and you want the result to be a list of floats.
>>
>> a = [float(n) for n in s1.split(',')]
>> b = [float(n) for n in s2.split(',')]
>>
>> But I suspect that I'm being overly optimistic...
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
 		 	   		  



More information about the Tutor mailing list