shanenin Posted May 26, 2006 Report Share Posted May 26, 2006 (edited) I am trying to make a backup solution for a business project of mine. I need a way to moniter my customers backups, make sure everything keeps working properly. My first thought was to set up something like vnc that would allow me to remotely check my clients computers. My wife pointed out, I may not want that kind of control. If something goes wrong the customer will blame me. That is not a very efficient use of my time, plus it would be a little intrusive to them.My next thought was to have a program do some simple checks of disk space, files saved and other things I would want to moniter. I could do all of this using python or some other simple scripting language(maybe vb script), then save the data to a text file. Here is where I am lost. I need to send that text file(the information) out of their computer to mine. I am not sure what mechanism would be needed for something like that. Any suggestions or direction to accomplish this would be appreciated. Thanks Edited May 26, 2006 by shanenin Quote Link to post Share on other sites
naraku9333 Posted May 26, 2006 Report Share Posted May 26, 2006 I don't know about python but in java it is fairly easy to write apps that read and write to sockets. The client could have a daemon running that does the specific checks that you want and if the conditions are met send an error code like STORAGE_LOW,etc... on the socket. You would need a daemon running listening on the port on your end for this to work and would also want a static ip (or domain name). Quote Link to post Share on other sites
Naming is hard Posted May 26, 2006 Report Share Posted May 26, 2006 (edited) Try Twisted, its a Module for Network protocols for Python ^^ i think thats what you'd want right?Heres a book about Twisted, its alittle dated but its the only one,http://www.amazon.com/gp/product/059610032...5Fencoding=UTF8 Edited May 26, 2006 by Naming is hard Quote Link to post Share on other sites
shanenin Posted May 26, 2006 Author Report Share Posted May 26, 2006 thanks guys, I will google both "socket programming" and "twisted" . Quote Link to post Share on other sites
Naming is hard Posted May 26, 2006 Report Share Posted May 26, 2006 Lol, i have a bad habit of rushing my post and then editing, 72times ^^ theres more info there. Quote Link to post Share on other sites
jcl Posted May 26, 2006 Report Share Posted May 26, 2006 Twisted might be overkill for the client. It sounds like you need something like sendfile(): snarf bytes from this file and stuff them down this socket.Actually you might be to able save yourself a lot of trouble if you use HTTP. Install a server, write a little CGI script to receive files, and use Python's HTTP convenience libraries to POST the file from the client to the server. The client might be slightly more complex but the server could be reduced to teensy script to store the incoming file. Quote Link to post Share on other sites
shanenin Posted May 26, 2006 Author Report Share Posted May 26, 2006 (edited) I have not done any programming in about 6 months. I am feeling very rusty, basic syntax is giving me a hard time. None the less, I am trying to follow this tutorial. http://72.14.207.104/search?q=cache:LDExvZ...us&ct=clnk&cd=1 Edited May 26, 2006 by shanenin Quote Link to post Share on other sites
shanenin Posted May 26, 2006 Author Report Share Posted May 26, 2006 I am getting a syntax error when trying to run the client software. below is what I have#!/usr/bin/env python# this is the clientimport socketimport sys# create a sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# connect to serverhost = sys.argv[1] # server addressport = int(sys.argv[2]) # server ports.connect((host, port))s.send(sys.argv[3]) # send test string# read echoi = 0while(1): data = s.recv(1000000) # read up to 1000000 bytes i += 1 if (i < 5): print data if not data: # if end of data, leave loop break print ’received’, len(data), ’bytes’# close the connections.close()I am getting this error message. I am getting a warning, buut I do not think that is the problemC:/Python24/pythonw.exe -u "C:/Documents and Settings/shane/tmc.py"sys:1: DeprecationWarning: Non-ASCII character '\x92' in file C:/Documents and Settings/shane/tmc.py on line 24, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details File "C:/Documents and Settings/shane/tmc.py", line 24 print ’received’, len(data), ’bytes’ ^SyntaxError: invalid syntax Quote Link to post Share on other sites
shanenin Posted May 26, 2006 Author Report Share Posted May 26, 2006 I am not sure what the problem was. I deleted and retyped that last part of the code, but I used double quotes(I like the look) and it is working. Every now and again I get those odd sytax errors. retryping it the same way seems to solve them. It does not make any sence. Quote Link to post Share on other sites
jcl Posted May 27, 2006 Report Share Posted May 27, 2006 Looks like you were bitten by smart quotes. What editor did you use? Quote Link to post Share on other sites
shanenin Posted May 27, 2006 Author Report Share Posted May 27, 2006 (edited) What are smart quotes? Edited May 27, 2006 by shanenin Quote Link to post Share on other sites
jcl Posted May 27, 2006 Report Share Posted May 27, 2006 (edited) Smart quotes is a feature found in word processors and some text editors. Many character sets include a variety of quote-like characters: left and right quotation marks, apostrophes, primes, minute and second marks, etc. When you enter a quotation mark in an editor with smart quotes enabled it tries to determine from the context what kind of quote-like character you wanted and substitute that character in. In the code you posted the single quotes are actually right (closing) quotes from Microsoft's codepage 1252 character set (the default encoding on Windows). Edited May 27, 2006 by jcl Quote Link to post Share on other sites
shanenin Posted May 27, 2006 Author Report Share Posted May 27, 2006 Looks like you were bitten by smart quotes. What editor did you use?I was using drpython on my windows box. I think I just cut and pasted directly from this tutorial. Quote Link to post Share on other sites
jcl Posted May 27, 2006 Report Share Posted May 27, 2006 Ah. Yeah, whatever program they used to generate the PDF from the LaTeX source screwed up the quotes. Quote Link to post Share on other sites
shanenin Posted May 27, 2006 Author Report Share Posted May 27, 2006 (edited) Sending data directly from comuter to computer is really cool(I must be a geek). Here is what i am using for my server#!/usr/bin/env python# this is the server to send a text fileimport sysimport socket# creates a socketmySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )# the sys.argv() allows me to specifie a port to listen onmySocket.bind ( ( '', int(sys.argv[1]) ) )mySocket.listen ( 1 )# listening loopwhile True: conn, addr = mySocket.accept() print 'We have opened a connection with', addr print conn.recv ( 100 ) #conn.send ("") conn.close()this is the client I am using to recieve a text file#!/usr/bin/env python# client for sending text fileimport sysimport socket# following line read the file into a stringfile = open('send.txt', 'r')filetext = file.read()mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )# the sys.argv() allows me to give a port to use as a comand argumentmySocket.connect ( ( 'localhost', int(sys.argv[1]) ) )mySocket.send (filetext)mySocket.recv (1000)mySocket.close()Question:in some of the code I was looking at, they used this line, which I have commented out. What purpostse does it serveconn.send ("")edit added later//I tried to send a large file, my smb.conf. it recieved part of it(about the first 5 lines) then gave me an errorshane@mainbox ~ $ ./tmc2.py 6665Traceback (most recent call last): File "./tmc2.py", line 15, in ? mySocket.recv (10000)socket.error: (104, 'Connection reset by peer') Edited May 27, 2006 by shanenin Quote Link to post Share on other sites
Naming is hard Posted May 27, 2006 Report Share Posted May 27, 2006 Looks like you were bitten by smart quotes. What editor did you use?I was using drpython on my windows box. I think I just cut and pasted directly from this tutorial.never heard of Drpython, have you tried http://pythonide.stani.be/ ? Quote Link to post Share on other sites
shanenin Posted May 27, 2006 Author Report Share Posted May 27, 2006 (edited) now I am able to send my smb.conf file in full without error. I don't think I made any changes(not sure)edit added later//I think I changed this line, I had it set to lowprint conn.recv ( 100 )I think that sets the maximum amount of bytes the server will accept Edited May 27, 2006 by shanenin Quote Link to post Share on other sites
Naming is hard Posted May 27, 2006 Report Share Posted May 27, 2006 (edited) hmmm, im having problems getting this to work,the server tms.py gets up and running fine but with i try and send something with the client tmc.py i get this error""" \s.connect((host, port))File "(string)", line 1, in connectsocket.gaierror: (11001, 'getaddrinfo failed')""" Edited May 27, 2006 by Naming is hard Quote Link to post Share on other sites
shanenin Posted May 27, 2006 Author Report Share Posted May 27, 2006 could you have mad a typo is copying the source? If you want post it, I am curious to see what it looks like. Quote Link to post Share on other sites
Naming is hard Posted May 27, 2006 Report Share Posted May 27, 2006 (edited) could you have mad a typo is copying the source? If you want post it, I am curious to see what it looks like.hmmm, iv checked like 1923809 times but yeah ill post it, one secEdit:Client:# simple illustration client/server pair; client program sends a string# to server, which echoes it back to the client (in multiple copies),# and the latter prints to the screen# this is the clientimport socketimport sys# create a sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# connect to serverhost = sys.argv[1] # server addressport = int(sys.argv[2]) # server ports.connect((host, port))s.send(sys.argv[3]) # send test string# read echoi = 0while(1): data = s.recv(1000000) # read up to 1000000 bytes i += 1 if (i < 5): print data if not data: # if end of data, leave loop break print 'received', len(data), 'bytes'# close the connections.close()Server:# simple illustration client/server pair; client program sends a string# to server, which echoes it back to the client (in multiple copies),# and the latter prints to the screen# this is the serverimport socketimport sys# create a sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# associate the socket with a porthost = '' # can leave this blank on the server sideport = int(sys.argv[1])s.bind((host, port))# accept "call" from clients.listen(1)conn, addr = s.accept()print 'client is at', addr# read string from client (assumed here to be so short that one call to# recv() is enough), and make multiple copies (to show the need for the# "while" loop on the client side)data = conn.recv(1000000)data = 10000 * data# wait for the go-ahead signal from the keyboard (shows that recv() at# the client will block until server sends)z = raw_input()# now sendconn.send(data)# close the connectionconn.close() Edited May 27, 2006 by Naming is hard Quote Link to post Share on other sites
shanenin Posted May 27, 2006 Author Report Share Posted May 27, 2006 (edited) your code is working for me. I started your server like thispython tms.py 5000then I started the client like thispython tmc.py 127.0.0.1 5000 abcedit added//are you doing this on linux or windows?edit added//I can use either windows or linux, I also have tried both the ip 127.0.0.1, and localhost. They all work the same Edited May 28, 2006 by shanenin Quote Link to post Share on other sites
Naming is hard Posted May 28, 2006 Report Share Posted May 28, 2006 (edited) Hmmm, alright ill try again.Yeah, worked this time, thanks ^^ Edited May 28, 2006 by Naming is hard Quote Link to post Share on other sites
naraku9333 Posted May 28, 2006 Report Share Posted May 28, 2006 (edited) Since I need alot of practice I decided to write a couple little client/server apps in java (Idont know any python to help with your's).Server.java/* * Server.java * * Created on May 27, 2006, 11:15 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. *//** * * @author naraku */import java.net.Socket;import java.net.ServerSocket;import java.io.BufferedReader;import java.io.PrintWriter;import java.io.InputStreamReader;import java.io.IOException;import java.util.Scanner;import java.io.FileOutputStream;public class Server { /** Creates a new instance of Server */ public Server() { } public static void main(String[] args){ Socket clientSock = null; ServerSocket serverSock = null; int port = Integer.parseInt(args[0]); BufferedReader in = null; //Scanner stdIn = new Scanner(System.in); try{ serverSock = new ServerSocket(port); } catch(Exception e){ System.err.println("cannot listen on "+port+"\n"+e); } try{ clientSock = serverSock.accept(); System.out.println("connected");//debugging } catch(Exception e){ System.err.println("could not accept client connection\n"+e); } try{ in = new BufferedReader(new InputStreamReader(clientSock.getInputStream())); } catch(Exception e){ System.err.println("could not create reader stream\n"+e); } // try{ PrintWriter out = new PrintWriter(new FileOutputStream("testClass.txt")); String x; while((x = in.readLine())!=null){ System.out.println(x); out.println(x); out.close(); } } catch(IOException e){ System.err.println(e); } /* catch(FileNotFoundException ex){ System.err.println(ex); }*/ }} Client.java/* * Client.java * * Created on May 27, 2006, 11:15 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. *//** * * @author naraku */import java.net.Socket;import java.io.BufferedReader;import java.io.PrintWriter;import java.net.ServerSocket;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;public class Client { /** Creates a new instance of Client */ public Client() { } public static void main(String[] args){ Socket clientSock = null; BufferedReader input = null; Scanner stdIn = new Scanner(System.in); PrintWriter sockOutput = null; PrintWriter output = null; String host = args[0]; int port = Integer.parseInt(args[1]); try{ clientSock = new Socket(host, port); System.out.println("connected to "+host+" on port "+port);//debugging } catch(IOException e){ System.err.println("could not connect to server "+host+" on port "+port+"\n"+e); } try{ input = new BufferedReader(new InputStreamReader(clientSock.getInputStream())); sockOutput = new PrintWriter(clientSock.getOutputStream(), true); System.out.println("io streams created"); } catch(IOException e){ System.err.print("could not create IO streams\n"+e); } sockOutput.print(args[2]); sockOutput.flush(); /*while(){ }*/ }}The server must be run first and takes the port as an arg(or can be har coded), the client takes 3 args, the host of server, port number and a test string. The server receives specified string outputs it to stdOutput and writes it to a file called testClass.txt in the directory app is run. I used sun's KnockKnock example as a basis. Edited May 28, 2006 by naraku9333 Quote Link to post Share on other sites
shanenin Posted May 28, 2006 Author Report Share Posted May 28, 2006 I am not sure what went wrongshane@mainbox ~/java $ javac javas.javajavas.java:25: class Server is public, should be declared in a file named Server.javapublic class Server { ^javas.java:22: cannot resolve symbolsymbol : class Scannerlocation: package utilimport java.util.Scanner; ^2 errors Quote Link to post Share on other sites
jcl Posted May 28, 2006 Report Share Posted May 28, 2006 javac requires that the file name be identical to the name of the class it contains (plus the .java extension).As for the second error... I would guess that you're using an older JDK. Scanner was added in Java 1.5. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.