Hallo,
ich will über eine Socket(TCP) Verbindung mehrere Daten schicken.
Beispiel:
Server schickt "1", Client antwortet mit "2", Server wieder mit "3", der Client antwortet mit "4". Ich will dies mit einer Socket Verbindung machen, ohne diese zu schliessen. Hätte jemand ein Beispiel für mich?
Wenn Server und Client nur einmal senden über die Socket Verbindung, klappt es. Senden sie öfters, klappt es nicht.
lg Richi
Mehrere Daten über eine Socket Verbindung schicken
Hallo,
so hier der Code. Ich habe nur sehr selten was mit Sockets gemacht und wenn, dann nur sehr primitive Anwendungen. Das ist nur ein "Ich-hatte-nur-15-Min-zeit" Code.
so hier der Code. Ich habe nur sehr selten was mit Sockets gemacht und wenn, dann nur sehr primitive Anwendungen. Das ist nur ein "Ich-hatte-nur-15-Min-zeit" Code.
Code: Alles auswählen
class RequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print "%s wrote:" % self.client_address[0]
print self.data
tmp = self.data.split(":")
if tmp[0] == "unlink":
self.unlink(tmp[1])
elif tmp[0] == "rmdir":
self.rmdir(tmp[1])
elif tmp[0] == "symlink":
self.symlink(tmp[1], tmp[2])
elif tmp[0] == "rename":
self.rename(tmp[1], tmp[2])
elif tmp[0] == "link":
self.link(tmp[1], tmp[2])
elif tmp[0] == "chmod":
self.chmod(tmp[1], tmp[2])
elif tmp[0] == "truncate":
self.truncate(tmp[1], int(tmp[2]))
elif tmp[0] == "mknod":
self.mknod(tmp[1], tmp[2], tmp[3])
elif tmp[0] == "mkdir":
self.mkdir(tmp[1], tmp[2])
elif tmp[0] == "utime":
self.utime(tmp[1], tmp[2])
elif tmp[0] == "write_file":
self.write_file(tmp[1])
def unlink(self, path):
os.unlink("." + path)
def rmdir(self, path):
os.rmdir("." + path)
def symlink(self, path, path1):
os.symlink(path, "." + path1)
def rename(self, path, path1):
os.rename("." + path, "." + path1)
def link(self, path, path1):
os.link("." + path, "." + path1)
def chmod(self, path, mode):
os.chmod("." + path, mode)
def chown(self, path, user, group):
os.chown("." + path, user, group)
def truncate(self, path, len):
f = open("." + path, "a")
f.truncate(int(len))
f.close()
def mknod(self, path, mode, dev):
os.mknod("." + path, int(mode), dev)
def mkdir(self, path, mode):
os.mkdir("." + path, int(mode))
def utime(self, path, times):
os.utime("." + path, times)
def write_file(self, path):
if os.path.exists("." + path):
# Send a "Yes i have the File" back
self.request.send("true")
md5sum = get_md5sum(path)
# Send the MD5 Sum back
self.request.send(md5sum)
command = "patch -u -f .%s" % (path)
cmd_patch = os.popen(command, "w")
# Fetch the Diff File
while True:
self.data = self.request.recv(1024).strip()
if self.data != 0: break
#diff += self.data
cmd_patch.write(self.data)
cmd_patch.close()
else:
# Send a "No i don't have this File" back
self.request.send("false")
# Fetch the complete File
f = open("." + path, 'w')
#f = open(self.root + path, 'w')
while True:
self.data = self.request.recv(1024).strip()
if self.data != 0: break
f.write(self.data)
f.close()
class RequestSender():
def __init__(self, HOST, PORT):
self.HOST, self.PORT = HOST, PORT
self.connect()
def connect(self):
log("RS: Connecting Server...", 1)
try:
# Create a socket (SOCK_STREAM means a TCP socket)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server and send data
self.sock.connect((self.HOST, self.PORT))
except socket.error, err:
log("Error: %s %s" % (err[0], err[1]), 2)
def send(self):
log("RS: Sending Data...", 1)
try:
self.sock.send(self.data + "\n")
except socket.error, err:
log("Error: %s %s" % (err[0], err[1]), 2)
def unlink(self, path):
log("RS: unlink:%s" % path, 1)
self.data = "unlink:%s" % path
self.send()
self.sock.close()
def rmdir(self, path):
log("RS: rmdir:%s" % path, 1)
self.data = "rmdir:%s" % path
self.send()
self.sock.close()
def symlink(self, path, path1):
log("RS: symlink:%s:%s" % (path, path1), 1)
self.data = "symlink:%s:%s" % (path, path1)
self.send()
self.sock.close()
def rename(self, path, path1):
log("RS: rename:%s:%s" % (path, path1), 1)
self.data = "rename:%s:%s" % (path, path1)
self.send()
self.sock.close()
def link(self, path, path1):
log("RS: link:%s:%s" % (path, path1), 1)
self.data = "link:%s:%s" % (path, path1)
self.send()
self.sock.close()
def chmod(self, path, mode):
log("RS: chmod:%s:%s" % (path, mode), 1)
self.data = "chmod:%s:%s" % (path, mode)
self.send()
self.sock.close()
def chown(self, path, user, group):
log("RS: chown:%s:%s" % (path, user, group), 1)
self.data = "chown:%s:%s:%s" % (path, user, group)
self.send()
self.sock.close()
def truncate(self, path, len):
log("RS: truncate:%s:%s" % (path, str(len)), 1)
self.data = "truncate:%s:%s" % (path, str(len))
self.send()
self.sock.close()
def mknod(self, path, mode, dev):
log("RS: mknod:%s:%s:%s" % (path, mode, dev), 1)
self.data = "mknod:%s:%s:%s" % (path, mode, dev)
self.send()
self.sock.close()
def mkdir(self, path, mode):
log("RS: mkdir:%s:%s" % (path, mode) ,1 )
self.data = "mkdir:%s:%s" % (path, mode)
self.send()
self.sock.close()
def utime(self, path, times):
log("RS: utime:%s:%s" % (path, times), 1)
self.data = "utime:%s:%s" % (path, times)
self.send()
self.sock.close()
def write_file(self, path, tmp_path, md5sum):
log("RS: write_file:%s:%s:%s" % (path, tmp_path, md5sum), 1)
# send a "Here is a new File"
self.data = "write_file:%s" % (path)
self.send()
received = self.sock.recv(1024)
# "Yes i have this File!"
if received == "true":
log("RS: i know this file: %s" % (path), 1)
# "Here is the MD5 Sum of my File"
received = self.sock.recv(1024)
# Is the md5sum on all nodes the same?
if received == md5sum:
log("RS: md5sum: %s -> %s == %s" % (path, received, md5sum), 1)
# Send a "Yes the MD5 Sum is the same!"
self.data = "true"
self.send()
#command = "diff -u .%s .%s" % (path, tmp_path)
command = "diff -u .%s .%s" % (tmp_path, path)
log("RS: executing: %s" % (command), 1)
cmd_diff = os.popen(command, "w", 1024)
# Fetch the Diff File
while True:
self.data = cmd_diff.read().strip()
if self.data != 0: break
#diff += self.data
self.send()
cmd_diff.close()
else:
log("RS: md5sum: %s -> %s != %s" % (path, received, md5sum), 1)
# Send a "No the MD5 Sum is not the same!"
self.data = "false"
self.send()
log("RS: writing file: %s" % (path), 1)
f = open("." + path, 'rb') # open in binary mode
while True:
data = f.read(1024)
if len(data) == 0: break # end of file
self.data = data
self.send
# "No i don't have this File!"
elif received == "false":
log("RS: i don't know this file: %s" % (path), 1)
#f = open("." + path, "rb")
#data = f.read(1024)
#while data:
# self.data = data
# self.send
# data = f.read(1024)
log("RS: writing file: %s" % (path), 1)
f = open("." + path, 'rb') # open in binary mode
while True:
data = f.read(1024)
if len(data) == 0: break # end of file
self.data = data
self.send
# Well something runs wrong...
else:
pass
self.sock.close()