You are not logged in.
Well I'm working on a little script that uses sockets. The idea is, the user on one machine opens up the server. Then the other user on the different machine(or on the same machine) can execute system commands such as 'ls'. I'm having some trouble though. I'm testing it on my own machine at the moment so I open up the server and it outputs the server has started. Then I open the client and connect to the port. I get the raw_input prompt and I try typing 'ls' but the client hangs. If I stop the server with ctrl+c I get the output of ls on the client.
Server code:
import sys, socket
host = ''
port = 50105
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: ", port)
s.listen(1)
while (1):
conn, addr = s.accept()
print 'New connection from ', addr
try:
while True:
rc = conn.recv(2)
pipe = os.popen(rc)
rl = pipe.readlines()
fl = conn.makefile('w')
fl.writelines(rl[:-1])
fl.close()
except IOError:
conn.close()
Client:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = input('Port: ')
s.connect((host, port))
while (1):
cmd = raw_input('$ ')
s.send(cmd)
file = s.makefile('r', 0)
sys.stdout.writelines(file.readlines())
file.close()
Here is a sample of the server side:
break@arch:~/python$ python server.py
('Server started on port: ', 50105)
New connection from ('127.0.0.1', 34761)
Here is a sample of the client:
break@arch:~/python/$ python client.py
Port: 50105
$ ls
After I type ls and hit enter it just hangs. Then I ctrl+c the server and then I get the output of ls on the client.
Anybody know why this is happening?
Offline
try flushing your output after writing. it could just be it is buffering.
sys.stdout.writelines(file.readlines())
sys.stdout.flush()
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
try flushing your output after writing. it could just be it is buffering.
sys.stdout.writelines(file.readlines()) sys.stdout.flush()
Nope. Still hangs
Offline
Why not use twisted? Using sockets directly is generaly discuraged. Also you probably want subprocess.Popen instead of os.popen.
Last edited by Mr.Elendig (2011-01-20 15:14:13)
Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest
Offline
oh man. twisted. I wouldn't wish that on anyone that desires to retain their sanity.
Good thing I don't have much of that left, as my previous job was entirely focused on writing code with twisted.
I would recommend eventlet or gevent instead.
Last edited by cactus (2011-01-20 19:58:23)
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
oh man. twisted. I wouldn't wish that on anyone that desires to retain their sanity.
Good thing I don't have much of that left, as my previous job was entirely focused on writing code with twisted.I would recommend eventlet or gevent instead.
Any advantages of those over sockets?
Offline