views:

46

answers:

2

I'm currently dabbling in sockets.
I've got a jquery script that uses a very small flash swf file to establish a true socket connection. As a server I'd like to use python.

Everything works, but I can only send information to the server just once.

I've tried 2 pieces of python code for the server.
I guess since they both have the same problem the problem's not here.

# TCP server example
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 2000))
server_socket.listen(5)

print "TCPServer Waiting for client on port 2000"

while 1:
        client_socket, address = server_socket.accept()
        print "I got a connection from ", address
        while 1:
                data = client_socket.recv(512)
                print "RECIEVED:" , data

Or this one:

import socket
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 2000 ) )
mySocket.listen ( 1 )
while True:
   channel, details = mySocket.accept()
   print 'We have opened a connection with', details
   print channel.recv ( 100 )
   channel.close()

On the client side I use http://devpro.it/xmlsocket/
Sending a piece of data is as simple as executing

xmls.send('some text');

Which arrives, but after that it stops working.

Does anyone know a solution? Or possibly a better javascript/swf combination I could use?

+1  A: 

For communicating between flash and python, I use:

http://pyamf.org/

You can either set up a socket server, or better, a small django application. Then you can have secure authentication, tokens, cookies, and strong typing if you want it.

eruciform
+2  A: 

I think you need to 'listen' again after you close the connection. Also, Sockets deliver data as per the tcp specifications. You're not guaranteed to get all of any data sent in one socket read. I see nothing to perform multiple reads and assemble a complete 'message'

Jay
Indeed. The example of the server was just that: a very basic example. I saw a loop and thought it would go on forever. It was built to quit after one burst of data.Here's a very nice tutorial for a python server, with threading even: http://ilab.cs.byu.edu/python/threadingmodule.html
skerit