ocarina/src/core/ct/tcp.py

54 lines
1.3 KiB
Python

#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="bjschuma"
__date__ ="$Apr 1, 2010 12:52:37 PM$"
import socket
import ocarina
import thread
from ct import call
from ct import path
def send(port,song):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect( ("localhost", port) )
totalsent = 0
while totalsent < len(song):
sent = s.send(song[totalsent:])
if sent == 0:
raise RuntimeError, "Socket connection broken!"
totalsent += sent
def server():
#create an INET, STREAMing socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
#bind the socket to localhost at the user specified port
server.bind(("localhost", ocarina.vars["$port"]))
except:
# Bind to an os-assigned port if the user defined port fails
server.bind(("localhost",0))
# listen for connections with a queue size of 5
server.listen(5)
# Find what port we are connected to
port = server.getsockname()[1]
def watch(addr,port):
while 1:
#accept connections from outside
(clientsocket, address) = server.accept()
message = clientsocket.recv(4096)#print clientsocket, address
if path.exists(message):
call.load(message)
thread.start_new_thread(watch,("localhost",port))
# Return the port we are using
return port