Socket module, socket
1
1.1 server:
#! /Use/local/env python
#-*-Coding: UTF-8 -*-
Import socket
Ip_port = ('2017. 0.0.1 ', 127)
# Ip_port = ('0. 0.0.0 ', 9999) # All IP addresses
Sk = socket. socket ()
Sk. bind (ip_port)
Sk. listen (5)
While True:
Print ('server wainting ...')
Conn, add = sk. accept ()
Client_data = conn. recv (1024)
Print (str (client_data, encoding = 'utf-8 '))
Conn. sendall (bytes ('Don't answer, don't answer, don't reply ', encoding = 'utf-8 '))
# Conn. sendall (bytes ('do not answer, do not answer, do not answer', 'utf-8 '))
# Conn. sendall (bytes ('do not answer, do not answer, do not answer', 'utf8 '))
Conn. close ()
1.2 client:
#! /Use/local/env python
#-*-Coding: UTF-8 -*-
Import socket
Ip_port = ('2017. 0.0.1 ', 127)
Sk = socket. socket ()
Sk. connect (ip_port)
Sk. sendall (bytes ('request to occupy the earth, request to occupy the earth, request to occupy the global', encoding = 'utf-8 '))
Server_reply = sk. recv (1024)
Print (str (server_reply, encoding = 'utf-8 '))
Sk. close ()
2
2.1 server:
#! /Use/local/env python
#-*-Coding: UTF-8 -*-
Import socket
Ip_port = ('2017. 0.0.1 ', 127)
# Ip_port = ('0. 0.0.0 ', 9999) # All IP addresses
Sk = socket. socket ()
Sk. bind (ip_port)
Sk. listen (5)
While True:
Print ('server wainting ...')
Conn, add = sk. accept ()
Client_data = conn. recv (1024) # number of characters
Print (str (client_data, encoding = 'utf-8 '))
Conn. sendall (bytes ('Don't answer, don't answer, don't reply ', encoding = 'utf-8 '))
# Conn. sendall (bytes ('do not answer, do not answer, do not answer', 'utf-8 '))
# Conn. sendall (bytes ('do not answer, do not answer, do not answer', 'utf8 '))
While True:
Try:
Client_data2 = conn. recv (1024)
Failed t Exception:
Break
Conn. send (client_data2)
Conn. close ()
2.2 client:
#! /Use/local/env python
#-*-Coding: UTF-8 -*-
Import socket
Ip_port = ('2017. 0.0.1 ', 127)
Sk = socket. socket ()
Sk. connect (ip_port)
Sk. sendall (bytes ('request to occupy the earth, request to occupy the earth, request to occupy the global', encoding = 'utf-8 '))
Server_reply = sk. recv (1024)
Print (str (server_reply, encoding = 'utf-8 '))
While True:
User_input = input (">"). strip ()
Sk. sendall (bytes (user_input, encoding = 'utf-8 '))
Server_reply2 = sk. recv (1024)
Print (str (server_reply2, encoding = 'utf-8 '))
Sk. close ()
3 ssh_socket
3.1 server
#! /Usr/local/env python
#-*-Coding: UTF-8 -*-
Import socket, subprocess
Num = 100
Sk = socket. socket ()
Ip_port = ('0. 0.0.0 ', 9999)
Sk. bind (ip_port)
Sk. listen (5)
Count = 0
While True:
Count + = 1
Print ("server is waiting [% s]..." % count)
Conn, add = sk. accept ()
# Client_data = conn. recv (1024)
# Print (str (client_data, encoding = 'utf-8 '))
# Conn. sendall (bytes ('do not answer {3}', encoding = 'utf-8 '))
While True:
Client_data = conn. recv (num)
If not client_data:
Print ('cv is empty ')
Break
Required _str = str (client_data, encoding = 'utf-8 ')
Pai_call = subprocess. Popen (pai_str, shell = True, stdout = subprocess. PIPE)
Pai_res = pai_call.stdout.read ()
If len (FIG) = 0:
# If not exist _res:
Performance_res = B "has no output! \ N"
Conn. send (pai_res)
Conn. close ()
3.2 client
#! /Usr/local/env python
#-*-Encoding: UTF-8 -*-
Import socket
Num = 100
Sk = socket. socket ()
Ip_port = ('10. 100.11.211 ', 9999)
Sk. connect (ip_port)
While True:
User_input = input ('cmd> '). strip ()
If len (user_input) = 0:
Continue
Elif user_input = 'q ':
Break
Sk. sendall (bytes (user_input, encoding = 'utf-8 '))
Server_data = sk. recv (num)
Print (str (server_data, encoding = 'utf-8'), end = '')
While not (len (server_data) <num ):
Server_data = sk. recv (num)
Print (str (server_data, encoding = 'utf-8'), end = '')
Sk. close ()
4. Upload Big Data Over ssh
4.1 server
#! /Usr/local/env python
#-*-Coding: UTF-8 -*-
Import socket, subprocess
Num = 500
Sk = socket. socket ()
Ip_port = ('0. 0.0.0 ', 9999)
Sk. bind (ip_port)
Sk. listen (5)
Count = 0
While True:
Count + = 1
Print ("server is waiting [% s]..." % count)
Conn, add = sk. accept ()
# Client_data = conn. recv (1024)
# Print (str (client_data, encoding = 'utf-8 '))
# Conn. sendall (bytes ('do not answer {3}', encoding = 'utf-8 '))
While True:
Client_data = conn. recv (num)
If not client_data:
Print ('cv is empty ')
Break
Required _str = str (client_data, encoding = 'utf-8 ')
Pai_call = subprocess. Popen (pai_str, shell = True, stdout = subprocess. PIPE)
Pai_res = pai_call.stdout.read ()
If len (FIG) = 0:
# If not exist _res:
Performance_res = B "has no output! \ N"
Pai_res_size_msg = bytes ('send _ DATA_SIZE: % s' % (len (pai_res), encoding = 'utf-8 ')
Conn. send (pai_res_size_msg)
Conn. recv (50)
Conn. send (pai_res)
Conn. close ()
4.2 client
#!/usr/local/env python
# -*- encoding:utf-8 -*-
import socket
num = 500
sk = socket.socket()
ip_port = ('10.100.11.211', 9999)
sk.connect(ip_port)
while True:
user_input = input('cmd>').strip()
if len(user_input) == 0:
continue
elif user_input == 'q':
break
sk.sendall(bytes(user_input, encoding='utf-8'))
CMD_SIZE_MSG = sk.recv(50)
CMD_SIZE_MSG = str(CMD_SIZE_MSG, encoding='utf-8')
#CMD_RES_SIZE_MSG = 'SEND_DATA_SIZE:%s' %(len(cmd_res))
CDM_SIZE_GET = CMD_SIZE_MSG.split(':')
CMD_SIZE = 0
if CDM_SIZE_GET[0] == 'SEND_DATA_SIZE':
CMD_SIZE = int(CDM_SIZE_GET[1])
CMD_ACK = b'ready'
sk.send(CMD_ACK)
recv_size = 0
while recv_size < CMD_SIZE:
server_data = sk.recv(num)
recv_size += len(server_data)
print(str(server_data, encoding='utf-8'), end='')
#print(str(server_data, encoding='utf-8'), end='')
#while not (len(server_data) < num):
# server_data = sk.recv(num)
# print(str(server_data, encoding='utf-8'), end='')
sk.close()