Sending Data to Server using Python's Socket Module

2,764 views
Skip to first unread message

Güray Yildirim

unread,
Sep 2, 2012, 4:49:11 PM9/2/12
to kivy-...@googlegroups.com
Hi,
I am trying to make a little android program for practise. I want to send a message(which is "1") to my pc(there it is server)  from the android device. I write this code and make .apk file. It worked but when I press the button, it crashes. Here is the code and explonation:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.clock import Clock
import socket
import urllib

class MyPaintWidget(Widget):
   
pass

class MyPaintApp(App):
   
def build(self):
        baba
= Widget()
       
def bas1(ornek):
            buton
.text = "Baglaniyor..."
            soket
= socket.socket()
            sunucu
= urllib.urlopen("http://www.asdfgh.com/server.txt").read()
           
print sunucu
            soket
.connect((sunucu,4444))
            buton
.text = "Baglandi."
            soket
.send("1")
            soket
.close()
            buton
.text = "Yakildi."

       
def bas2(ornek):
            buton
.text = "Baglaniyor..."
            soket
= socket.socket()
            sunucu
= urllib.urlopen("http://asdfgh.com/server.txt").read()
           
print sunucu
            soket
.connect((sunucu,4444))
            buton
.text = "Baglandi."
            soket
.send("2")
           
print soket.recv(1024)
            soket
.close()
            buton
.text = "Sonduruldu."
       
def deneme(gelen):
           
Clock.schedule_once(bas1,1)
        buton
= Button(text = "Lambayi Yak")
        buton
.bind(on_press = deneme)
        buton2
= Button(text = "Lambayi Sondur")
        buton2
.x = 150; buton2.width = 300
        buton2
.bind(on_press = bas2)
        cocuk1
= MyPaintWidget()
        baba
.add_widget(cocuk1)
        baba
.add_widget(buton)
        baba
.add_widget(buton2)
       
return baba

if __name__ in ("__main__"):
   
MyPaintApp().run()

asdfgh.com represents my website which I will update it with my pc's current ip in order to the device can connect the true pc. Should I use "twisted" module or can I make this code work without crashing?

Thank you for your time.


deakblue

unread,
Sep 3, 2012, 12:49:29 AM9/3/12
to kivy-...@googlegroups.com

So this is just my experience developing a lot of networking oriented stuff on Kivy -- I am not an expert on internals, these are only my observations.

Twisted just works.  Without the reactor, you'll probably see a lot of crashes.

If you still want to go on your own, study the python behind urlrequest.py carefully:

I believe the key is to make sure your network communications are all scheduled into the event loop.  I believe this is what twisted gives you for free.

Best regards and good luck,
-db-

Mathieu Virbel

unread,
Sep 3, 2012, 7:37:16 AM9/3/12
to kivy-...@googlegroups.com
The user snippet should work, but without the error, it's hard to tell
why it's crashing.

About twisted, when you said "I believe the key is to make sure your
network communications are all scheduled into the event loop. I believe
this is what twisted gives you for free." -> it's not for free, we have
a support for twisted framework, and then, schedule automatically the
network event in the main loop.

https://github.com/kivy/kivy/blob/master/kivy/support.py#L133

Otherwise, the event is within the reactor thread. And then, it's up to
you to call you UI task/change in the main loop :)

Mathieu


Le 03/09/2012 06:49, deakblue a �crit :
> <http://www.asdfgh.com/server.txt>").read()
> printsunucu
> soket.connect((sunucu,4444))
> buton.text ="Baglandi."
> soket.send("1")
> soket.close()
> buton.text ="Yakildi."
>
> defbas2(ornek):
> buton.text ="Baglaniyor..."
> soket =socket.socket()
> sunucu =urllib.urlopen("http://asdfgh.com/server.txt
> <http://asdfgh.com/server.txt>").read()
> printsunucu
> soket.connect((sunucu,4444))
> buton.text ="Baglandi."
> soket.send("2")
> printsoket.recv(1024)
> soket.close()
> buton.text ="Sonduruldu."
> defdeneme(gelen):
> Clock.schedule_once(bas1,1)
> buton =Button(text ="Lambayi Yak")
> buton.bind(on_press =deneme)
> buton2 =Button(text ="Lambayi Sondur")
> buton2.x =150;buton2.width =300
> buton2.bind(on_press =bas2)
> cocuk1 =MyPaintWidget()
> baba.add_widget(cocuk1)
> baba.add_widget(buton)
> baba.add_widget(buton2)
> returnbaba
>
> if__name__ in("__main__"):
> MyPaintApp().run()
> |
>
> asdfgh.com <http://asdfgh.com> represents my website which I will
> update it with my pc's current ip in order to the device can connect
> the true pc. Should I use "twisted" module or can I make this code
> work without crashing?
>
> Thank you for your time.
>
>
> --
>
>
>

deakblue

unread,
Sep 3, 2012, 3:49:18 PM9/3/12
to kivy-...@googlegroups.com
Hi Mathieu,

Sorry, I think I misspoke.   I should have written that if you use Twisted via Kivy support, twisted communication events 'just work' without much fuss. Otherwise, I had a similar problem as the OP where a lot of my socket communications would result in crashes.

But Twisted through support has been great,
-Will
  

On Monday, September 3, 2012 7:37:12 AM UTC-4, Mathieu Virbel wrote:
The user snippet should work, but without the error, it's hard to tell
why it's crashing.

About twisted, when you said "I believe the key is to make sure your
network communications are all scheduled into the event loop.  I believe
this is what twisted gives you for free." -> it's not for free, we have
a support for twisted framework, and then, schedule automatically the
network event in the main loop.

https://github.com/kivy/kivy/blob/master/kivy/support.py#L133

Otherwise, the event is within the reactor thread. And then, it's up to
you to call you UI task/change in the main loop :)

Mathieu


Le 03/09/2012 06:49, deakblue a �crit :

Güray Yildirim

unread,
Sep 3, 2012, 6:03:22 PM9/3/12
to kivy-...@googlegroups.com
Thank you so much, I will try to use twisted. I think it will make everything I want because I only try to transfer small numbers.
Thanks again for answers and for your time.

3 Eylül 2012 Pazartesi 14:37:12 UTC+3 tarihinde Mathieu Virbel yazdı:
The user snippet should work, but without the error, it's hard to tell
why it's crashing.

About twisted, when you said "I believe the key is to make sure your
network communications are all scheduled into the event loop.  I believe
this is what twisted gives you for free." -> it's not for free, we have
a support for twisted framework, and then, schedule automatically the
network event in the main loop.

https://github.com/kivy/kivy/blob/master/kivy/support.py#L133

Otherwise, the event is within the reactor thread. And then, it's up to
you to call you UI task/change in the main loop :)

Mathieu


Le 03/09/2012 06:49, deakblue a �crit :
Reply all
Reply to author
Forward
0 new messages