netstat -aon
#Ethan_s.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket, os, sys, time, subprocess # Import socket module
from subprocess import Popen, PIPE
reload(sys)
sys.setdefaultencoding('utf8')
#0================================== #http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
port = 60001 # Reserve a port for your service.
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
print 'Server listening....'
#1==================================
while True: #Loop for listening
conn, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
print('Server received', conn.recv(1024)) #R0 Hello server!, repr() like as str()
#2==================================
try:
while conn:
data = conn.recv(4096) #R1, 4096 is recommand
print ("Receiving...: "+data)
p=subprocess.Popen(data,shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
stdout,stderr =p.communicate()
send_data = str("\nServer got msg: \n===========START================\n"+
"cmd: "+data+"\n"+
"stdout: \n"+stdout+"\n"+
"stderr: \n"+stderr+"\n"+
"\n===========END================\n")
conn.sendall(send_data) #S1, buffer不足
#while (send_data):
# conn.send(send_data)
print("Sending... "+send_data)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except socket.error as msg:
print "Socket Error: %s" % msg
except TypeError as msg:
print "Type Error: %s" % msg
finally:
print('Successfully !')
#print('Sending finished')
#conn.send('Thank you for connecting\n')
conn.close()
print '\n\n\nServer listening....'
"""
Know issue buffer not ready 如果傳太多data會lose data
"""
#Ethan_c.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket, os, sys, time # Import socket module
from subprocess import Popen #http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
reload(sys)
sys.setdefaultencoding('utf8')
#0==================================
port = 60001
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
# Reserve a port for your service.
#1==================================
s.connect(('10.110.145.250', port)) #s.connect((host, port))
#10.110.146.77
#10.110.136.52
#10.110.136.159
s.send("Hello server!") #S0
#2==================================
try:
while True :
remote_cmd = raw_input("please input the cmd (default is dir): \n")#"please input the cmd (default is dir):", "dir"
if remote_cmd.lower() == "":
recv_data = s.recv(1024)
print ("\nReceive... \n"+ recv_data)
elif remote_cmd.lower() == "exit" or remote_cmd.lower() == "q":
s.send("Close by client")
s.close()
else:
s.send(remote_cmd) #S1
print('Sending... ',repr(remote_cmd))
time.sleep(1) #delay
recv_data = s.recv(1024) #R1, buffer not enough
if recv_data:
print ("\nReceive... \n"+ recv_data)
recv_data = s.recv(1024) # keep waiting for data coming. Bug at here
if not recv_data:
s.close()
break
time.sleep(1)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except socket.error as msg:
print "Socket Error: %s" % msg
except TypeError as msg:
print "Type Error: %s" % msg
finally:
#print("Sever: " +s.recv(1024))
s.close()
print('connection closed')
#p=Popen('received_file.bat')
#stdout,stderr =p.communicate()
"""
Know issue buffer not ready 如果傳太多data會lose data
"""
# -*- coding: UTF-8 -*-
import socket, os, sys, time # Import socket module
from subprocess import Popen #http://www.bogotobogo.com/python/python_network_programming_server_client_file_transfer.php
reload(sys)
sys.setdefaultencoding('utf8')
#0==================================
port = 60001
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
# Reserve a port for your service.
#1==================================
s.connect(('10.110.145.250', port)) #s.connect((host, port))
#10.110.146.77
#10.110.136.52
#10.110.136.159
s.send("Hello server!") #S0
#2==================================
try:
while True :
remote_cmd = raw_input("please input the cmd (default is dir): \n")#"please input the cmd (default is dir):", "dir"
if remote_cmd.lower() == "":
recv_data = s.recv(1024)
print ("\nReceive... \n"+ recv_data)
elif remote_cmd.lower() == "exit" or remote_cmd.lower() == "q":
s.send("Close by client")
s.close()
else:
s.send(remote_cmd) #S1
print('Sending... ',repr(remote_cmd))
time.sleep(1) #delay
recv_data = s.recv(1024) #R1, buffer not enough
if recv_data:
print ("\nReceive... \n"+ recv_data)
recv_data = s.recv(1024) # keep waiting for data coming. Bug at here
if not recv_data:
s.close()
break
time.sleep(1)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
except socket.error as msg:
print "Socket Error: %s" % msg
except TypeError as msg:
print "Type Error: %s" % msg
finally:
#print("Sever: " +s.recv(1024))
s.close()
print('connection closed')
#p=Popen('received_file.bat')
#stdout,stderr =p.communicate()
"""
Know issue buffer not ready 如果傳太多data會lose data
"""