[Tutor] Socket question.

Dude, WHOA! sxkorean at gmail.com
Wed May 16 22:02:51 CEST 2007


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


More information about the Tutor mailing list