728x90

라즈베리파이 자동컴파일 시스템을 소켓과 pyqt를 이용해서 구현하는 과정에서 쓰이던 코드입니다.

 

서버는 pyqt(client)측에서 빌드버튼을 누르면 빌드가 되면서 빌드의 결과를 클라이언트측으로 다시 보내주는 코드입니다.

Server(서버)

import socket
import os
import sys
import time
import re
import threading

HOST = socket.gethostname()
PORT = 6322
ADDR = (HOST, PORT)
BUFF_SIZE = 1024

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(ADDR)
threads = []
server_socket.listen(10)
_lock = threading.Lock()
def build_thread_data(client_socket):
	_lock.acquire()
	#_lock2.acquire()
	print("first file socket start")
	with open('main.cpp','wb') as f:
		print('main.cpp opened')
		while True:
			print('receiving data from client ...')
			data = client_socket.recv(10000)
			if not data:
				f.close()
				print("not file")
				break
			f.write(data)

	print("first file socket complete")
	BUILD = "sudo g++ main.cpp -o main 2> tf.txt"
	os.system(BUILD)
	#time.sleep(2)
	print("<<<<<<<<<<<<<<<<<<<<<<<build complite>>>>>>>>>>>>>>>>>>")
	fw = open("tf.txt", "rt", encoding='utf-8')
	line = fw.readline()

	if not line:
		fc = open("output.txt", "wt", encoding='utf-8')
		fc.write("complete build !!!")
		fc.close()
		fw.close()

	else:
		fc = open("output.txt", "wt", encoding='utf-8')
		copydata = fw.read()
		parse = re.sub('[‘’]', '', copydata)
		fc.write(parse)
		fc.close()
		fw.close()
	print("convert cpp to txt file")
	#time.sleep(1)

	#remove_tf_file="sudo rm -f tf.txt"
	#os.system(remove_tf_file)
	_lock.release()
	client_socket.close()

def build_thread_data2(client_socket):
	_lock.acquire()
	#_lock.acquire()
	f2 = open('output.txt','rb')
	while True:
		data2 = f2.read(10000)
		while data2:
			client_socket.send(data2)
			data2 = f2.read(10000)
		if not data2:
			f2.close()
			_lock.release()
			client_socket.close()
			break
	#remove_txt_file="sudo rm -f output.txt"
	#os.system(remove_txt_file)
	#time.sleep(2)




while True:
	#server_socket.listen(10)
	print("waiting for connections ....")
	(client_socket, (host, port)) = server_socket.accept()
	print('connection from', (host, port))
	print("check thread mode :")
	recv_msg = client_socket.recv(1024)
	msg = recv_msg.decode('utf-8')
	if msg == '1':
		print("client choose mode 1")
		msg == '0'
		build_thread = threading.Thread(target=build_thread_data, args = (client_socket,))
		build_thread.daemon = True
		build_thread.start()
		threads.append(build_thread)
		for build_thread in threads:
			build_thread.join()
	elif msg == '2':
		print("client choose mode 2")
		msg == '0'
		build_thread2 = threading.Thread(target=build_thread_data2, args = (client_socket,))
		build_thread2.daemon = True
		build_thread2.start()
		threads.append(build_thread2)
		for build_thread2 in threads:
			build_thread2.join()
		
	for t in threads:
		t.join()
			

 

 

클라이언트는 서버측에 빌드를 요청하면서 빌드할 파일이나 코드를 보내게 됩니다.

빌드결과는 텍스트파일로 서버측으로부터 받습니다.

 

Client(클라이언트)

import socket
import os
import sys
import time
import re
import threading

HOST = socket.gethostname()
PORT = 6322
ADDR = (HOST, PORT)

client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_socket.connect(ADDR)
print("connect server complete")
time.sleep(1)
send_msg='1'

client_socket.sendall(send_msg.encode('utf-8'))
print("send file to server start!")
f=open('main.cpp','rb')
while True:
	SendData=f.read(10000)
	while SendData:
		client_socket.send(SendData)
		SendData=f.read(10000)
	if not SendData:
		f.close()
		client_socket.close()
		break

print("first file socket complete")
#time.sleep(2)
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_socket.connect(ADDR)
send_msg='2'

client_socket.sendall(send_msg.encode('utf-8'))
print("receive file to server start!")

with open('output.txt','wb') as f2:
	print("receive file opened...")
	while True:
		print("receive output.txt data...")
		data2 = client_socket.recv(10000)
		if not data2:
			f2.close()
			break
		f2.write(data2)
print("complete file receive")
client_socket.close()

 

 

실행결과 화면

 

왼쪽 서버 오른쪽 클라이언트

pyqt관련 코드는 같이진행한 친구의 블로그를 참고해주시면 됩니다.

 

imalright.tistory.com/8

 

[합동]PyQt와 소켓통신을 이용한 파일전송프로그램

제가 사용할 PyQt는 QPushButton, QTextBrowser, QTextEdit, QLabel입니다. 소켓통신부분은 twobeach.tistory.com/21 투비치 yolo,c언어,라즈베리파이,소켓 등등 개발자 툴 다루는 블로그 twobeach.tistory.com 위..

imalright.tistory.com

 

728x90

+ Recent posts