Re: [Tutor] Socket question.

2007-05-16 Thread Martin Walsh
Hey Dude :)

Dude, WHOA! wrote:
> kinda thing. The problem is that the client I wrote doesn't receive
> data and display it, and it also only executes single word commands.

> Server side:
> #!/usr/bin/env python



>   try:
>   echo = Popen(command, stdout=PIPE).stdout.read()

On a linux system (and perhaps Windows as well), the type of 'command'
seems to be important and changes the behavior of Popen -- whether it be
string or sequence. If you pass a 'command' as a string that includes
arguments (ex. 'ls -l'), the above will raise an exception. I'm not sure
if the same applies to Windows. You could try passing command as a list
or tuple (ex command.split(), ['ls', '-l'], or similar), or add
'shell=True' to the Popen call.

> Client:
> #!/usr/bin/env python



>   send = raw_input('Send: ')
>   sock.send(send)
>   sock.recv(2048)

Try 'print sock.recv(2048)'.

>   sock.close()

HTH,
Marty
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket question.

2007-05-16 Thread Alan Gauld

"Dude, WHOA!" <[EMAIL PROTECTED]> wrote

> kinda thing. The problem is that the client I wrote doesn't receive
> data and display it, and it also only executes single word commands.

> Server side:
> #!/usr/bin/env python
> import socket
> from subprocess import *
> IP = 'localhost'
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.bind((IP, 1234))

You are using localhost so it will only work with the client on
the same PC and using port 1234.

> Client:
> IP = raw_input('IP: ')
> PORT = input('PORT: ')

So there is no point in asking the user, it has to be
IP = 127.0.0.1
PORT = 1234

Anything else will fail.

You can look at the Network programming topic on my
tutorial for some similar examples if you like.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket question.

2007-05-16 Thread Dude, WHOA!
I thought I'd try my hand at sockets, and tried to make a telnet-ish,
kinda thing. The problem is that the client I wrote doesn't receive
data and display it, and it also only executes single word commands.
Thanks in advance.

Server side:
#!/usr/bin/env python
import socket
from subprocess import *
IP = 'localhost'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((IP, 1234))
sock.listen(5)
while True:
log = open("log.log", "a")
channel, details = sock.accept()
command = channel.recv(2048)
channel.send(command)
log.write(str(details))
log.write("\n")
log.close()
print "New connection logged."
try:
echo = Popen(command, stdout=PIPE).stdout.read()
channel.send(echo)
except:
print 'Eh?'
channel.send('Eh?')
channel.close()

Client:
#!/usr/bin/env python
import socket
print '''
-
 Weak Insecure Shell
-
'''
IP = raw_input('IP: ')
PORT = input('PORT: ')
while 1:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((IP,PORT))
send = raw_input('Send: ')
sock.send(send)
sock.recv(2048)
sock.close()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] socket question

2005-09-13 Thread Alan G

> When I receive a 4-bytes integer using socket.recv, it is stored in 
> a
> string. How to convert this string to a integer?

Look at the struct module.
Its unpack method takes a format string which defines how the data
should be interpreted.

Have a look at the section at the end of my  file handling topic for
some info and examples of usage:

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] socket question

2005-09-13 Thread Kent Johnson
ray wrote:
> When I receive a 4-bytes integer using socket.recv, it is stored in a 
> string. How to convert this string to a integer?

Take a look at unpack() in module struct.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] socket question

2005-09-13 Thread ray
When I receive a 4-bytes integer using socket.recv, it is stored in a 
string. How to convert this string to a integer?
Thanks  in advance.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor