******** client.py
#!/usr/bin/python
#importing socket module for socket creation and sys for user input/output
import socket
import sys
## to accept user input of hostname and port
(host,port)=(sys.argv[1],int(sys.argv[2]))
print "host is",host
print "port is",port
#creating and a socket and connecting to host via port
socket=socket.socket()
socket.connect((host, port))
#prompting user for input
input=raw_input("user>")
while input != "bye":
socket.send(input)
data=str(socket.recv(65535))
print data
input=raw_input("user>")
socket.close()
******** server.py
#!/usr/bin/python
import socket
import sys
#import subprocess
import commands
socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server=("localhost",12000)
socket.bind(server)
socket.listen(5)
conn,clientip=socket.accept()
var=1
while var==1:
print "conn accepted"
data=str(conn.recv(65635))
print "data received"
if data != "" and data != "bye":
# p = subprocess.Popen(['ls'],stdout=subprocess.PIPE)
result=commands.getoutput(data)
print "printing result:" ,result
print "sending result"
if result =="":
result = " "
conn.send(result)
print "result sent"
if data == "bye":
conn.close
socket.close
******** result
siakas-MacBook-Pro:PYTHON siaka$ ./client.py localhost 12000
host is localhost
port is 12000
user>ls -lh
total 56
-rwxr-xr-x 1 siaka staff 534B Jun 14 05:54 client.py
-rwxr-xr-x 1 siaka staff 1.2K Jun 14 03:46 client2.py
-rwxr-xr-x 1 siaka staff 86B Jun 14 04:01 cmd.py
-rwxr-xr-x 1 siaka staff 154B Jun 14 03:39 getargs.py
-rwxr-xr-x 1 siaka staff 594B Jun 13 20:47 python.py
-rwxr-xr-x 1 siaka staff 1.5K Jun 14 03:45 serv1.py
-rwxr-xr-x 1 siaka staff 703B Jun 14 05:56 server.py
user>touch file{1..10}
user>ls -lh
total 56
-rwxr-xr-x 1 siaka staff 534B Jun 14 05:54 client.py
-rwxr-xr-x 1 siaka staff 1.2K Jun 14 03:46 client2.py
-rwxr-xr-x 1 siaka staff 86B Jun 14 04:01 cmd.py
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file1
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file10
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file2
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file3
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file4
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file5
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file6
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file7
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file8
-rw-r--r-- 1 siaka staff 0B Jun 14 18:29 file9
-rwxr-xr-x 1 siaka staff 154B Jun 14 03:39 getargs.py
-rwxr-xr-x 1 siaka staff 594B Jun 13 20:47 python.py
-rwxr-xr-x 1 siaka staff 1.5K Jun 14 03:45 serv1.py
-rwxr-xr-x 1 siaka staff 703B Jun 14 05:56 server.py
user>bye
siakas-MacBook-Pro:PYTHON siaka$
No comments:
Post a Comment