django - 尝试使用Django中的套接字创建聊天应用程序

标签 django python-3.x sockets django-templates

我正在使用django创建聊天应用程序以创建聊天应用程序,这将有助于在用户和管理员之间建立聊天。我有一个用python编写的代码,将有助于通过终端进行通信,但我无法找到解决方案,我如何附加到前端,例如html,css。

server.py

# server.py

def do_some_stuffs_with_input(input_string):  
    """
    This is where all the processing happens.

    Let's just read the string backwards
    """

    print("Processing that nasty input!")
    return input_string[::-1]

def client_thread(conn, ip, port, MAX_BUFFER_SIZE = 4096):

    # the input is in bytes, so decode it

    input_from_client_bytes = conn.recv(MAX_BUFFER_SIZE)

    # MAX_BUFFER_SIZE is how big the message can be
    # this is test if it's sufficiently big
    import sys
    siz = sys.getsizeof(input_from_client_bytes)
    if  siz >= MAX_BUFFER_SIZE:
        print("The length of input is probably too long: {}".format(siz))

    # decode input and strip the end of line
    input_from_client = input_from_client_bytes.decode("utf8").rstrip()

    res = do_some_stuffs_with_input(input_from_client)
    print("Result of processing {} is: {}".format(input_from_client, res))

    vysl = res.encode("utf8")  # encode the result string
    conn.sendall(vysl)  # send it to client
    conn.close()  # close connection
    print('Connection ' + ip + ':' + port + " ended")
import time, socket, sys


print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
host = socket.gethostname()
ip = socket.gethostbyname(host)
port = 1234
s.bind((host, port))
print(host, "(", ip, ")\n")
name = input(str("Enter your name: "))

s.listen(1)
print("\nWaiting for incoming connections...\n")
conn, addr = s.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")

s_name = conn.recv(1024)
s_name = s_name.decode()
print(s_name, "has connected to the chat room\nEnter [e] to exit chat room\n")
conn.send(name.encode())

while True:
    message = input(str("Me : "))
    if message == "[e]":
        message = "Left chat room!"
        conn.send(message.encode())
        print("\n")
        break
    conn.send(message.encode())
    message = conn.recv(1024)
    message = message.decode()
    print(s_name, ":", message)

client.py
# client.py
import time, socket, sys

print("\nWelcome to Chat Room\n")
print("Initialising....\n")
time.sleep(1)

s = socket.socket()
shost = socket.gethostname()
ip = socket.gethostbyname(shost)
print(shost, "(", ip, ")\n")
host = input(str("Enter server address: "))
name = input(str("\nEnter your name: "))
port = 1234
print("\nTrying to connect to ", host, "(", port, ")\n")
time.sleep(1)
s.connect((host, port))
print("Connected...\n")

s.send(name.encode())
s_name = s.recv(1024)
s_name = s_name.decode()
print(s_name, "has joined the chat room\nEnter [e] to exit chat room\n")

while True:
    message = s.recv(1024)
    message = message.decode()
    print(s_name, ":", message)
    message = input(str("Me : "))
    if message == "[e]":
        message = "Left chat room!"
        s.send(message.encode())
        print("\n")
        break
    s.send(message.encode())

最佳答案

查看本教程-本教程是关于使用django和vue.js开发聊天应用程序的。

https://danidee10.github.io/2018/01/01/realtime-django-1.html

与其他框架(如react或angular)的连接类似。只需 checkout 教程即可。希望对您有帮助。

关于django - 尝试使用Django中的套接字创建聊天应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53846699/

相关文章:

Python 将列表转换为集合,大 O

php - WEB SOCKET PHP 概念

python - 多个 django 站点上的单点登录

django - 从 Django JSONField 获取值

django - 更改 Django 管理列表中的行颜色

mysql - 将 1.7.4 升级到 django 1.8 south.db.mysql 错误,未使用 South

python-3.x - Azure 函数 - "Did not find any initialized language workers"

Python 计数元音

java - 检测套接字断开?

java - java中的setSoTimeout方法改变哪个定时器?