Python, Web app won't save to file. Server Side - c#

If you want a quick version scroll down to the "Edit|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.
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. (not to mention then ill have to add SQL into the middleman app).
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. I can get saving to work in IDLE but not in this app on the server.
(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
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). 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?

Check out the tempfile module, for example, tempfile.mkstemp() with a known directory. Have your "middleman app" poll (or perhaps using inotify) the directory for new files, process the file, and then delete it when done.

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.

Related

Can't download my own executeable c#

I just made a small and simple windows form. I uploaded the .exe to mediafire so anyone could download it, but when someone(or even myself) tries to download it: windows defender instantly deletes it, because it thinks it is a virus. Is this a problem with the code or does it has to do with something else?
Hash it to check it was not modified
Check with some digest algorithm like SHA-2 that the file you download from the site is actually the exact same that you uploaded.
If it is not the same, something fishy has happened to your file when on a trip to the internet. I wouldn't try to open it, and try to find another service to host your file.
Sign your executable with a certificate
Invest in some code signing certificate.
Some are free, and for beginning and test pourposes you can create it yourself and self-sign it, but it will still be frowned upon by anti-malwares and your system.
Other comments
Also, some code operations are considered "dangerous" by some antivirus, because theese operations are much more oftne found in exploits attempts than in real commercial code. I remember, when I was playing with simple console C++ code while learning, I did some unsafe operation with a simple string char[]. On my school computer, the result executable was automatically deleted with the McAfee guard within seconds of compilation...

Getting code from Windows Azure VM

OK, so my hard disk just crashed. Big deal. All my web dev code that was on it went along with it, and now I'm running ddrescue on Ubuntu trying to recover whatever data I can recover. The hard disk keeps disconnecting and sometimes it can quit responding for a long time so it's really a pain in the ass.
Anyway, back to the main topic--I have my web dev code which was packaged and uploaded to Azure; now what I'm wondering is if it's possible to obtain all my .cs files from the VM. I noticed approot and siteroot folders, but all I saw were the views, the .asax file, some other misc, stuff, nothing with the .cs extension.
Is there any way I can get a copy of the code I packaged? or (as a last resort) any way to get the .cspkg file and work from there?
The site you are seeing on the web role and inside the cspkg file is the output of the compile, so you can't get the original .cs files out of them. That said, you can use a tool like Reflector, Just Decompile, or a variety of other decompilers out there to reverse engineer your compiled bits into something that will be very close to the original C# code (not I'm assuming this is your own code, or code that doesn't have a provision against reverse engineering). This at least will let you use the bits on the webrole to get the majority of your code back, then review it to see how good a job it did.
Note, you can open the cspkg file. It's just a zip file. You can rename with a .zip file extension and open it up, but you won't find the .cs files in there. The only time you find this to be the case is if you have multiple websites within a single web role. The default packager for Windows Azure doesn't compile the additional sites, only packages up all the files in their root directory. Not at all helpful for actual deployments really, but this won't likely help you.
You are likely well ahead of me on this, but I'd recommend using a personal source control system of some sort to avoid this issue in the future.

How can I get data off of my Windows Phone 7 emulator

I have a unit testing framework for WP7 and it runs on the phone. The results are fairly hard to read so I am writing them to an XDocument.
My question is, how can I then get this XML file off of the phone and onto my desktop where I can actually analyze the results?
What I have done so far is to put a Debugger.Break() line right after where the summary xml is created. I can then copy/paste the xml out of VS or inspect it right in the debugger. The problem is though, that if you don't already have a debugger attached (which is good when lots of ExpectedException tests) Debugger.Attach() seems to not work, also manually attaching VS to the emulator processes seems to do nothing.
I tried running the emulator with some extra command line parameters so I could try to see if I could get it to use my actual hard drive as it's own disk but I couldn't seem to get it to work...
PS it's probably not reasonable to pop open a new process such as a webserver to listen for this data. I know how to do that, I would just rather not.
So how the heck do you get stuff off of these phones??
Have a look at this article about emulator automation from Justin Angel.
It includes details on how to remotely read and write files from/to emulator/device isolated storage.
As you pointed out the other alternative would be to have the applciation send the results to a [local] web server.
The article by Justin Angel is really great, but unfortunately his file-based solution does not work on the final RTM versions of the CoreCon API. Microsoft has simply removed that functionality from the native conman layer.
I've been in the same situation as you and have contemplated various ways to get data out of the device, but in the end only one thing seems to work: as you suggest yourself, pass data to an external webservice.
That solution is less than ideal not only because it takes some effort, but also because of a few caveats:
your app must be granted ID_CAP_NETWORKING capability
network-traffic seems disallowed in the Application_Closing event, and maybe elsewhere too
On the bright side I found that webrequests from the phone, both hardware and emulator, were really fast so the approach works very well (our app is EQATEC Profiler for WP7).

C# Cannot write to Application.ExecutablePath, some boxes i can some i cannot? uninstall from this

So I have been writing to
Environment.SpecialFolder.ApplicationData
this data file, that upon uninstall needs to be deleted. I am using Innos Setup to build my installer. It works great for me. So my data file hangs out in the above path and I do that cause when I used to try to write it to
Application.ExecutablePath
certain boxes I tested it on would throw a nasty error at me trying to write data there. I do research and somehow its not always writable and its how i came up with the Environment.SpecialFolder.ApplicationData
That is why my data file now resides in the SpecialFolder.ApplicationData. Trouble is if the user uninstalls and reinstalls I need that file gone. It might be a short coming of my knowledge of Innos but I cannot figure out how to know where that file will be to tell innos that.
So then I thought I had a clever solution: Innos can run a file when its done uninstalling, so I had my program create this file "uninstallData.bat" that says:
del "the file in my special folder application data path"
and I wrote it out to drumroll
Application.ExecutablePath
(yes it was a while in development and I had forgot it was't doable.)
So of course I am back to square one, I need to write a file to a path Innos knows about {app} and I need it to be able to delete my data file in the SpecialFolder... i don't care how I do it i just need that file gone.
Are there other Environment. or Application. approches I have missed? Maybe somewhere that is viewable by an uninstaller AND can be written to?
As an aside, I am not sure why my box I develop on can write to the application folder no issue, but it cannot on other boxes... weird.
Any input would be great sorta lost as to how to crack this nut.
The environment location is in the user profile. If there are multiple users on the machine, and they all run the application then a copy of the file will be in each profile.
The path also depends on the OS.
Regardless, the current user's app data location is pointed to by %APPDATA% and %LOCALAPPDATA%. These Windows environment variables should be available within Innos.
Appliccation.ExecutablePath is not writable per standard defintions - the program files folder should never be manipulated by running applications. Ther area number of special folders for that. Nice that you finally found.... what is properly documented by Microsoft for a LONG time now (minimum 10 years).
I suggest you get a proper installer - WIX comes to my mind. Your problem is totally unrelated to C# - it seems to be totally a "crappy installer" issue. Or provide a PROGRAM (not bat file) to run at uninstall. What exatly is your problem there?

how to know when download is finished

Hi I'm creating online shop. In this shope people online must be buy files with zip extension. They pay with their credit cards or other methods get key and download product. How can I know when they finish product download?
Thanks
Unfortunatelly there is no really good way to do this as some clients might not download the file at once (e.g. Downloadmanagers split the download into several parralel part downloads).
Options are:
If it is very important to you that it can only be downloaded once: You could
simply not support resuming. Then you
can log if the file has entirely been
downloaded (as soon as the last byte
has been sent). This might work well if the download is small.
Otherwise you could offer some grace
data (we usually allow to download
clients to download 5 times the size
of the real download) and log every
download attempt.
You should NOT just count the bytes downloaded (because the download might be disrupted). And NOT just determine if all sections have been downloaded once (also because the download might be disrupted)
Just to clarify: All this means that you have to write your own download handler (fileserver).
you can use custom file server that works on either http or ftp and have it send a notification once the client received the last file fragment.
all other options are problematic; the client might download the file using a download manager,so you cannot even register for any browser event, if there was any.
A custom server application seems indeed a solution for this,
or possibly some kind of scripting.
A normal http server does not notify the end of a connection,
but possibly, if you generate the output in a cgi/php/asp/* script,
you read the file in cgi/php/asp/* scripting language and
send it to the output. when you reach the end of the file, you
do the notification, and then end the script.
When you do it that way, it will only detect fully downloaded files,
and if the connection gets interrupted half-way, it would not mark
the file as downloaded.
a 'cgi-script' can be a compiled c program, (or any other langauge
for that matter). Compiled code anyways. A compiled program
would give better performance then a interpreted script solution.

Categories

Resources