Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cant connect stably to FlaskSocketIO sever #201

Open
tranvinhphuc111996 opened this issue Nov 15, 2018 · 0 comments
Open

Cant connect stably to FlaskSocketIO sever #201

tranvinhphuc111996 opened this issue Nov 15, 2018 · 0 comments

Comments

@tranvinhphuc111996
Copy link

tranvinhphuc111996 commented Nov 15, 2018

Hi All !
Im using Flask and Flask SocketIO to make sever . it run well when i using JS client, but when im using socketIO_client (python) to connect to sever , it cant connect to sever ! . please check this code help me ! Thanks .

my sever have ip :104.131.98.89
sever:
`
from flask import Flask, request, jsonify, render_template, session, redirect, escape
from werkzeug.utils import secure_filename
from datetime import datetime
import os
import sqlite3
from flask_socketio import SocketIO, emit
import json
import threading
DATABASE_FILE = 'database.db'
app = Flask(name, static_url_path='/static')
app.secret_key = os.urandom(12)
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = './static/image'
socketio = SocketIO(app, async_mode=None)
def connectdb():
conn = sqlite3.connect(DATABASE_FILE, isolation_level = None)
return conn
def querydb(conn, query, v = tuple()):
c = conn.cursor()
c.execute(query, v)
return c.fetchall()

def logged_in():
if not session.get('logged_in'):
return 0
return 1

@app.route('/')
def index():
if logged_in():
return render_template('index.html')
else:
return redirect('/dang_nhap')
@app.route('/dang_nhap', methods=['GET', 'POST'])
def dang_nhap():
if request.method == 'GET':
return render_template('dang_nhap.html')
else:
username = request.form.get('username')
password = request.form.get('password')
conn = connectdb()
r = querydb(conn, r"SELECT * FROM Administrator WHERE username=? AND password=?", (username, password))
if r:
session['logged_in'] = True
return redirect('/')
else:
return render_template('dang_nhap.html')
@app.route('/static/')
def static_file(filename):
return app.send_static_file(filename)

@app.route('/json/scan_the', methods=['POST'])
def scan_the():
json_post_data = request.get_json()
CardID = json_post_data['CardID']
DeviceID = json_post_data['DeviceID']
FingerID = json_post_data['FingerID']
ImageID = json_post_data['ImageID']
EntryTime = json_post_data['EntryTime']
conn = connectdb()
r = querydb(conn, r"SELECT * FROM Employee WHERE CardID=?", (CardID, ))
if len(r) > 0:
respone = {'status': 1}
querydb(conn, r"INSERT INTO Work(DeviceID,CardID,FingerID,ImageID,EntryTime) VALUES (?,?,?,?,?)", (DeviceID,CardID,FingerID,ImageID,EntryTime, ))
name_string = querydb(conn, r"SELECT EmployeeName FROM Employee WHERE CardID=?",(CardID,))

	post_web_data = json.dumps({"NameEmploy": name_string,"EntryTime": EntryTime,"ImageID": ImageID})
	print 'post_web_data:', post_web_data
	socketio.emit('my_response', post_web_data , namespace='/tracking')
else:
	respone = {'status': 0}
	try:
		querydb(conn, r"INSERT INTO UnRegistered(CardID) VALUES (?)", (CardID, ))
	except:
		pass
conn.commit()
conn.close()
return jsonify(respone)

@app.route('/Tracking', methods=['POST', 'GET'])
def tracking():
return render_template('Tracking.html')
def allowed_file(filename):
return '.' in filename and
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/rev_image', methods=['POST', 'GET'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
post_web_data = json.dumps({"Image_status":"true"})
socketio.emit('image_response', post_web_data , namespace='/tracking')
return 'Success'
return 'Success'

@app.route('/them_nhan_vien', methods=['POST', 'GET'])
def them_nhan_vien():
if not logged_in():
return redirect('/dang_nhap')

if request.method == 'GET':
	return render_template('them_nhan_vien.html')
else:
	post_data 			= request.form
	CardID 				= post_data['mathe'] 
	EmployeeName 		= escape(post_data['hoten'])
	EmployeeBirthDate 	= datetime.strptime(post_data['ngaysinh'], '%d-%m-%Y')
	print(post_data)
	conn 				= connectdb()
	r 					= querydb(conn, r"INSERT INTO Employee(EmployeeName, CardID, EmployeeBirthDate) VALUES(?,?,?)", (EmployeeName, CardID, EmployeeBirthDate))
	return render_template('them_nhan_vien.html')

@app.route('/json/xoa_nhan_vien', methods=['POST'])
def xoa_nhan_vien():
if not logged_in():
return redirect('/dang_nhap')

json_post_data 		= request.get_json()
CardID 				= json_post_data['mathe']
conn 				= connectdb()
r 					= querydb(conn, r"DELETE FROM Employee WHERE CardID=?", (CardID,))
return jsonify({'status': 1})

@app.route('/json/sua_thong_tin_nhan_vien', methods=['POST'])
def sua_thong_tin_nhan_vien():
if not logged_in():
return redirect('/dang_nhap')

json_post_data 		= request.get_json()
CardID 				= json_post_data['mathe'] 
EmployeeName 		= escape(json_post_data['hoten'])
EmployeeBirthDate 	= datetime.strptime(json_post_data['ngaysinh'], '%d-%m-%Y')
conn 				= connectdb()
r 					= querydb(conn, r"UPDATE Employee SET EmployeeName=?,EmployeeBirthDate=? WHERE CardID=?", (EmployeeName, EmployeeBirthDate, CardID))
return jsonify({'status': 1})

@app.route('/them_thiet_bi', methods=['POST', 'GET'])
def them_thiet_bi():
if not logged_in():
return redirect('/dang_nhap')

if request.method == 'GET':
	return render_template('them_thiet_bi.html')
else:
	post_data 	= request.form
	DeviceID  	= post_data['deviceid']
	description = escape(post_data['description'])
	conn 		= connectdb()
	r 			= querydb(conn, r"INSERT INTO Device(DeviceID, Description) VALUES(?,?)", (DeviceID, description))
	return render_template('them_thiet_bi.html')

@socketio.on('Ping')
def handle_ping():
print('Ping !!!')
`

In client :
from socketIO_client import SocketIO, LoggingNamespace import time socket = SocketIO('http://104.131.98.89',5000,LoggingNamespace) socket.emit('Ping')

it Sometimes connect , but it sometimes dont connect, it stuck in this pic and it sometimes have nothing.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant