-
Notifications
You must be signed in to change notification settings - Fork 5
/
client_tcp_threads.py
58 lines (49 loc) · 2.23 KB
/
client_tcp_threads.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
# -*- coding: utf-8 -*-
__author__ = "Voronin Denis Аlbertovich"
# connection-oriented client
import socket
import datetime
import time
import base64
##host = 'fhoc.no-ip.org'
host = '192.168.0.156'
port = 1800
data = 'client'
send_interval = 2 # in seconds
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(to_host,to_port):
try:
client_socket.connect((to_host, to_port))
print("[Connection established]")
except Exception:
print('[Connection error: ' + to_host + ":" + str(port)+']')
client_socket.close()
pass
def run():
global data
with open('logo.png', "rb") as image_file:
data = base64.b64encode(image_file.read())
print('---------------------------------------------------------------------------------------------')
data_send = bytes('info//'+'pc_name:' + socket.gethostname() + 'ip:' + socket.gethostbyname(socket.gethostname()) + 'dt:' + (datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')), 'UTF-8')
print("Send session data: " + str(socket.gethostname() + 'ip:' + socket.gethostbyname(socket.gethostname()) + 'dt:' + (datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S'))))
client_socket.send(data_send)
try:
data_input = (client_socket.recv(1024).decode("UTF-8"))
except Exception:
pass
print("Waiting confirm session data...: ", data_input)
if data_input.find('sess_ok',0) != -1:
print("Send main data: " + str(data))
client_socket.send(bytes('data//' + str(data),'UTF-8'))
try:
data_input = (client_socket.recv(1024).decode("UTF-8"))
except Exception:
pass
print("Waiting confirm main data..." + data_input)
client_socket.close()
print('---------------------------------------------------------------------------------------------')
while True:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect(host, port)
run()
time.sleep(send_interval)