配置SocketServer以使用Django

时间:2017-01-24 05:47:23

标签: python django socketserver

我有一个与TCP端口通信的设备。它附带一个只能在Windows中运行的桌面应用程序。但我想在Linux服务器和django应用程序中使用它。我试图与SocketServer库进行通信,它工作正常,但我在套接字服务器中使用djangos ORM时遇到问题。我需要将数据存储到数据库。

2 个答案:

答案 0 :(得分:0)

我制作了一款专为设备数据而设计的django应用。在应用程序中,我有一个名为socket_server.py的文件。它的代码如下:

`

import SocketServer
import django
from models import Demo

class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
    # self.request is the TCP socket connected to the client
    self.data = self.request.recv(1024).strip()
    print "{} wrote:".format(self.client_address[0])
    print self.data
    demo = Demo(
        text=self.data
    )
    demo.save()
    self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
    server.serve_forever()` 

当我尝试运行socket_server.py文件时,我得到了以下内容 Traceback (most recent call last): File "socket_server.py", line 5, in <module> from .models import Demo ValueError: Attempted relative import in non-package niloy@niloy-PC ~/PycharmProjects/django-practice/books $ python socket_server.py Traceback (most recent call last): File "socket_server.py", line 5, in <module> from models import Demo File "/home/niloy/PycharmProjects/django-practice/books/models.py", line 9, in <module> class Publisher(models.Model): File "/home/niloy/PycharmProjects/django-practice/books/models.py", line 10, in Publisher name = models.CharField(max_length=30) File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1043, in __init__ super(CharField, self).__init__(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 166, in __init__ self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__ self._setup(name) File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 39, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. niloy@niloy-PC ~/PycharmProjects/django-practice/books $ python socket_server.py Traceback (most recent call last): File "socket_server.py", line 5, in <module> from models import Demo File "/home/niloy/PycharmProjects/django-practice/books/models.py", line 9, in <module> class Publisher(models.Model): File "/home/niloy/PycharmProjects/django-practice/books/models.py", line 10, in Publisher name = models.CharField(max_length=30) File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1043, in __init__ super(CharField, self).__init__(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 166, in __init__ self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__ self._setup(name) File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 39, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

答案 1 :(得分:0)

为了使用独立的 Django 功能,您应该在导入后使用 django.setup() 手动设置 Django。 请参阅文档 here