从Python中从Android套接字接收的字符串中删除转义字符

时间:2017-11-22 04:07:06

标签: android python sockets

import sqlite3
import nltk
from nltk.tokenize import word_tokenize
from socket import *
import sys
import socket

HOST = "192.168.43.123" #local host  IT SHOWS AN ERROR WHEN I USE MY PC's IP ADDRESS  47.11.229.90 127.0.0.1 192.168.43.123
PORT = 9999 #open port 18000 for connection
s = socket.socket(AF_INET, SOCK_STREAM)
try:
    s.bind((HOST, PORT))
except socket.error as e:
    print("Bind error, Error code: ",e.strerror,"Message: ",e.strerror)
    sys.exit(1)
print("Bind successful")
s.listen(10) #how many connections can it receive at one time
print("In the loop")
while True:
    connect_device, addr = s.accept()  # accept the connection
    print("Connected by: ", addr)
    #msg1 = 'Thank you for connecting' + "\r\n"
    #connect_device.send(msg1.encode('ascii'))
    data = connect_device.recv(4096)  # how many bytes of data will the server receive
    data = data.decode('ascii','ignore')
    print("Received: ", repr(data))

    conn = sqlite3.connect("ciao.db")
    c=conn.cursor()
    #string = "Hey tell me something about dengue"
    #string2 = string.lower()
    string = repr(data)
    string2 = string.lower()
    list_items = word_tokenize(string2)
    list_items2 = []
    escape_chars = ['\x00\x00','\x00\x01','\x00\x02','\x00\x03','\x00\x04','\x00\x05','\x00\x06','\x00\x07','\x00\x08','\x00\x09']
    for st in list_items:
        for j in escape_chars:
            if j in st:
                print(j,"List item is ",st)
                list_items2.append(st.strip(j))
    print("List items = ",list_items2)
    Greetings = ['hi', "hey"]
    c.execute("SELECT Disease_Name FROM ciao")
    data = c.fetchall()
    data2 = []
    for i in data:
        for j in i:
            if j is not None:
                data2.append(j)

    print("data2 = ",data2)
    msg = ""
    print("loop starts")
    for i in list_items2:
        print(i)
        print("Data = ",data2)

        if i in data2:
            if 'symptoms' in list_items2:
                c.execute("SELECT Symptoms from ciao WHERE Disease_Name = (?)",(i,))
                sym = c.fetchall()
                for j in sym:
                    for k in j:
                        msg += k + " "
            if 'remedial' in list_items2:
                c.execute("SELECT Remedial_Measures from ciao WHERE Disease_Name = (?)", (i,))
                rem = c.fetchall()
                for j in rem:
                    for k in j:
                        msg += k + " "
            if 'medicines' in list_items2:
                c.execute("SELECT Medicines from ciao WHERE Disease_Name = (?)", (i,))
                rem = c.fetchall()
                for j in rem:
                    for k in j:
                        msg += k + " "
            if 'diagnostic' in list_items2:
                c.execute("SELECT Diagnostic_tests from ciao WHERE Disease_Name = (?)", (i,))
                rem = c.fetchall()
                for j in rem:
                    for k in j:
                        msg += k + " "
            if 'hospitals' in list_items2:
                c.execute("SELECT Related_Hospitals from ciao WHERE Disease_Name = (?)",(i,))
                rem = c.fetchall()
                for j in rem:
                    for k in j:
                        msg += k + " "
            if not('symptoms' in list_items2) and not('remedial' in list_items2) and not('medicines' in list_items2) and not('diagnostic' in list_items2) and not('hospitals' in list_items2):
                c.execute("SELECT * from ciao WHERE Disease_Name = (?)",(i,) )
                rem = c.fetchall()
                for j in rem:
                    for k in j:
                        msg += k + " "

        else:
            if i in Greetings:
                if len(list_items) == 1 or len(list_items) == 0:
                    msg = "Hello I am Pi"
    if msg == "":
        msg = "Sorry"
    print(msg)
    connect_device.sendall(msg.encode('ascii'))
    #connect_device.send(msg.encode('utf-8'))
    conn.close()
    connect_device.close()

s.close()

我已经使用套接字将Android客户端连接到python服务器,每当我从android向python服务器发送消息时,都会添加一些转义字符。 例如: 如果我发送'你好,你好吗?' 我的python收到' \ x00 \ x05hello你好吗?'

在此,前七个字符每次都是常见的,即' \ x00 \ x0'每次从0到9随机生成最后一个字符。 我尝试使用循环删除这些字符,但它不起作用。 请检查我的代码并提出任何更改建议。谢谢。

0 个答案:

没有答案