Sponsored By

Sign in to follow this  
shanenin

Send Info Out Of Machine

Recommended Posts

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 by shanenin

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 client
import socket
import sys

# create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect to server
host = sys.argv[1] # server address
port = int(sys.argv[2]) # server port
s.connect((host, port))
s.send(sys.argv[3]) # send test string

# read echo
i = 0
while(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 connection
s.close()

I am getting this error message. I am getting a warning, buut I do not think that is the problem

C:/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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Looks like you were bitten by smart quotes. What editor did you use?

Share this post


Link to post
Share on other sites

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 by jcl

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Ah. Yeah, whatever program they used to generate the PDF from the LaTeX source screwed up the quotes.

Share this post


Link to post
Share on other sites

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 file

import sys
import socket

# creates a socket
mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
# the sys.argv() allows me to specifie a port to listen on
mySocket.bind ( ( '', int(sys.argv[1]) ) )
mySocket.listen ( 1 )

# listening loop
while 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 file

import sys
import socket

# following line read the file into a string
file = 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 argument
mySocket.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 serve

conn.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 error

[email protected] ~ $ ./tmc2.py 6665
Traceback (most recent call last):
File "./tmc2.py", line 15, in ?
mySocket.recv (10000)
socket.error: (104, 'Connection reset by peer')

Edited by shanenin

Share this post


Link to post
Share on other sites

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 low

print conn.recv ( 100 )

I think that sets the maximum amount of bytes the server will accept

Edited by shanenin

Share this post


Link to post
Share on other sites

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 connect

socket.gaierror: (11001, 'getaddrinfo failed')

"""

Edited by Naming is hard

Share this post


Link to post
Share on other sites

could you have mad a typo is copying the source? If you want post it, I am curious to see what it looks like.

Share this post


Link to post
Share on other sites
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 sec

Edit:

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 client
import socket
import sys

# create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect to server
host = sys.argv[1] # server address
port = int(sys.argv[2]) # server port
s.connect((host, port))

s.send(sys.argv[3]) # send test string

# read echo
i = 0
while(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 connection
s.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 server
import socket
import sys

# create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# associate the socket with a port
host = '' # can leave this blank on the server side
port = int(sys.argv[1])
s.bind((host, port))

# accept "call" from client
s.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 send
conn.send(data)

# close the connection
conn.close()

Edited by Naming is hard

Share this post


Link to post
Share on other sites

your code is working for me. I started your server like this

python tms.py 5000

then I started the client like this

python tmc.py 127.0.0.1 5000 abc

edit 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 by shanenin

Share this post


Link to post
Share on other sites

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 by naraku9333

Share this post


Link to post
Share on other sites

I am not sure what went wrong

[email protected] ~/java $ javac javas.java
javas.java:25: class Server is public, should be declared in a file named Server.java
public class Server {
^
javas.java:22: cannot resolve symbol
symbol : class Scanner
location: package util
import java.util.Scanner;
^
2 errors

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this