简体   繁体   English

Python,Web应用程序不会保存到文件。 服务器端

[英]Python, Web app won't save to file. Server Side

If you want a quick version scroll down to the "Edit|1|" 如果要使用快速版本,请向下滚动到“编辑| 1 |” part. 部分。

I HAVE done a bit of searching on this and can't seem to figure it out. 我已经对此进行了一些搜索,但似乎无法弄清楚。 I have a Webserver and a Minecraft server on the same machine (it never takes large loads so its ok) and I need the user to be able to put some input (in an html form), have that input saved in a file on the server, handled by a middleman app (which I've already got done and is in c#) and the middleman app interacts with the minecraft server. 我在同一台机器上有一个Web服务器和一个Minecraft服务器(它永远不会承受很大的负载,这样就可以了),我需要用户能够输入一些输入(以html格式),并将该输入保存在文件中服务器,由一个中间人应用程序处理(我已经完成并且在c#中),并且该中间人应用程序与minecraft服务器进行交互。

Now everything I either have done before or know how to do. 现在,我以前做过的一切或知道如何做。 The only problem is saving the content of the form into a temporary text file so that the middleman app can do its magic. 唯一的问题是将表单的内容保存到一个临时文本文件中,以便中间人应用程序可以发挥其魔力。 I thought about using SQL (since its on the server cause minecraft uses it for stats) but in my opinion its a bit overkill for something that will only be there for a few seconds. 我考虑过使用SQL(因为它在服务器上的原因是minecraft将其用于统计数据),但在我看来,对于仅存在几秒钟的某些东西来说,它有点过分了。 (not to mention then ill have to add SQL into the middleman app). (更不用说必须将SQL添加到中间人应用中)。

I don't really care where on the server the file ends up since I'll likely hard code the location into the middleman app and it will be deleted after the middleman app reads it. 我不太在意文件在服务器上的最终位置,因为我可能会将位置硬编码到Middleman应用程序中,并且在Middleman应用程序读取该文件后将其删除。 I can get saving to work in IDLE but not in this app on the server. 我可以保存以在IDLE中工作,但不能在服务器上的此应用程序中工作。

(I know this code won't take in anything from a form, this was just written as a test to save files) (我知道这段代码不会从表单中获取任何内容,这只是作为保存文件的测试而编写的)

import os

name = "none";

def editFile():
    workfile = open("edit.x",'w')
    workfile.write(name)
    workfile.close()


def application(environ, start_response):
    status = "200 OK"
    output = "Testificate (Feature will be up shortly)"

    response_headers = [('Content-type', 'text/plain'),('Content-Length',str(len(output)))]


    start_response(status, response_headers)
    return [output]

Extra Server info... 额外的服务器信息...

Hardware: Sufficiently powerful 硬件:足够强大

WebServer: Wamp w/ Python Module installed on Apache WebServer:带Apache模块上安装的Python模块的Wamp

Also here is a link to what runs with that code Click HERE. 另外,还有指向该代码的内容的链接。单击此处。

Edit|1|: I guess I didn't get much into the problem (It was late lastnight when I wrote this). 编辑| 1 |:我想我对这个问题没有太多了解(我写这篇文章时是昨晚很晚)。 Basically Any type of file will do. 基本上任何类型的文件都可以。 I want Ideally the simplest to implement. 我希望理想的是最简单的实施。 The above code has no result On the server. 上面的代码在服务器上没有结果。 It never creates the file nor can it read from the file (if I create it manually). 它永远不会创建文件,也不会从文件中读取文件(如果我手动创建的话)。 I've been working at it for about a day and a half now. 我已经为此工作了大约一天半。 I'm really just hoping Its a mistake on my part. 我真的只是希望它是我的错。 Could the server config in wamp dis-allow the creation of files via Python? Wamp中的服务器配置是否可以禁止通过Python创建文件?

Check out the tempfile module, for example, tempfile.mkstemp() with a known directory. 检出tempfile模块,例如具有已知目录的tempfile.mkstemp() Have your "middleman app" poll (or perhaps using inotify ) the directory for new files, process the file, and then delete it when done. 让“中间人应用”轮询(或使用inotify )新文件的目录,处理该文件,然后在完成后将其删除。

So I realized what I did while at lunch today. 所以我意识到今天午餐时我做了什么。 (facepalming myself in the process). (在此过程中让自己变脸)。 The reason it wasn't working was the fact that I used backslashes, not forward slashes. 它不起作用的原因是我使用了反斜杠,而不是正斜杠。 So here is the working version of the above (I used triple quotes just to be sure). 因此,这是上述代码的工作版本(为了确定,我使用了三引号)。 I also changed it to be absolute path. 我也将其更改为绝对路径。

def editFile():
    workfile = open("""C:/wamp/www/test/edit.txt""",'w')
    workfile.write(name)
    workfile.close()

After that it worked flawlessly. 之后,它可以完美地工作。 So if anyone else makes that small mistake here is a reminder. 因此,如果其他任何人犯了这个小错误,则在此提醒您。 lol Thanks to "mhawke" for the reply on tempfiles Its good info to have in either case. 大声笑感谢“ mhawke”对tempfiles的答复在任何一种情况下,它都有很好的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM