Error trying to run first python script on IIS - iis

I followed the steps in a respose to how to Set up Python for IIS :
Python on IIS: how?
But when I write my script and try running it it fails on the first line. I get this error message:
HTTP Error 502.2 - Bad Gateway The specified CGI application
misbehaved by not returning a complete set of HTTP headers. The
headers it did return are " File
"C:\Development\Python\helloworld.py", line 1 print 'Content-Type:
text/plain' ^ SyntaxError: invalid syntax ".
This is my frist attempt at python. What is going wrong?

So your example helloworld.py needs to be changed to:
print('Content-Type: text/plain')
print('')
print('Hello, world!')
Python 3 changed print to use function call syntax (which can be used in python 2).

Related

Getting unexpected token by running code in JSR223 Sampler(groovy) in jmeter

While running the below code in JSR223 SAMPLER, i am getting this error
Response code:500
Response message:javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script989.groovy: 1: unexpected token: < # line 1, column 1.
<HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy"
The code starts like this:
< HTTPSamplerProxy guiclass="HttpTestSampleGui" testclass="HTTPSamplerProxy"
testname="Checkout Payment Info/Place Order${__property(activeAdminThread)}(${testLabel})" enabled="true">
< boolProp name="HTTPSampler.postBodyRaw">true
Error appearing for the very first line itself..what wrong in it ,plz help?
This is not a valid Groovy code, it seems to be a part of JMeter .jmx test plan (pure XML file)
This HTTPSamplerProxy is what's being used under the hood of the normal HTTP Request sampler so I would recommend going to where did you get this code, downloading it adding .jmx extension and opening it in JMeter GUI.
To solve the above issue you have to upgrade your JDK and JRE.
Download the JDK and JRE and setup the Environment variable path
JAVA_HOME - JDK/bin
path - JRE/bin

python subprocess call from wsgi

When I run the exact same function from the python3 interpreter vs, apache via mod wsgi, they both run error free but one returns the command text from apache the stout is simply always blank. Again I am running the exact same function.
Backgorund, I want to run svn update on some code to do so I am using subprocess to simply call "svn update /path/to/repo"
def update():
p1=subprocess.Popen(["svn", "update", "/var/www/myrepocode"],stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
(ret, stderr) = p1.communicate(timeout=10)
return (str(ret.decode('utf-8')))
when i run from the python3 shell or another python 3 script called from the shell
from test import update
print(update())
it works fine and i get
"Updating '/var/www/myrepocode':\nAt revision 27.\n"
when I have a wsgi script and execute it from accessing the web page
def application(environ, start_response):
...
output = output + "---"+update() +"---"
...
response_headers = [('Content-type', 'text/html'), ('Content-Length',
str(len(output)))]
start_response(status, response_headers)
return [output.encode('utf-8')]
I get an empty string:
------
I do not have a python virtual environment, just normal python3 and there are no errors thrown either. I suspect this has to do with the user running it but I really don't know. setting shell=True doesn't change anything either. Any help is greatly appreciated.
Your update method does not return nor print stderr . Maybe you should check for errors first by adding the following line in the update method.
print(stderr)
If errors are produced, can you post them?

URLWatch: Using urls.yaml from a remote URL

I am trying to update my current urlwatch.py file with the ability to call the urls.yaml file from a remote server. I've tried to modify the urls_yaml variable with the URL I would like to use without any luck. (Generic URL is being used for the example):
urls_yaml = 'http://pathtourl.com/urls.yaml')
The standard output message is:
You need to create http://pathtourl.com/urls.yaml in order to use urlwatch.
Use "urlwatch --edit" to open the file with your editor.
The error message is:
File "/Users/mattstone/Python/urlwatch-2.2/urlwatch", line 51
urls_yaml = 'http://pathtourl.com/urls.yaml')
^
SyntaxError: invalid syntax
In the above standard output, I'm confused on how I would create the yaml on a remote server when it's not installed there.

Imaplib syntax error in Terminal but not in IDLE

I'm starting out learning Python 3 on Linux and am attempting to write a script to notify me when I receive emails from certain people. The following code works fine in IDLE3 but I am encountering an invalid syntax error when I try to run it from Terminal:
tcd = imaplib.IMAP4_SSL('outlook.office365.com')
tcd.login(addr, password)
tcd.select('INBOX', readonly=False)
The Terminal error is as follows:
File "mailcheck.py", line 22
tcd = imaplib.IMAP4_SSL('outlook.office365.com')
^
SyntaxError: invalid syntax
Any advice would be greatly appreciated!
Turns out I was missing a closing parenthesis at the end of the line before this. Rookie mistakes >:-/

Unable to run python cgi scripts using CGIHTTPRequestHandler in Python 3.3

I am a noob; trying to create and use a simple webserver in Python that executes CGI scripts written in Python. I am using Windows XP and Python v3.3.0. I have a "myserver" directory which contains "myserver.py","sample.html" and the directory "cgi-bin" which in turn contains "cgi_demo.py"
myserver.py
from http.server import HTTPServer
from http.server import CGIHTTPRequestHandler
port = 8080
host = '127.0.0.1'
server_address = (host,port)
httpd = HTTPServer(server_address,CGIHTTPRequestHandler)
print("Starting my web server on port "+str(port))
httpd.serve_forever()
cgi_demo.py
import cgi
import cgitb; cgitb.enable()
print("Content-type: text/html")
print
print("<html><body>")
for i in range(0,100):
print(i,"<br>")
print("</body></html>")
Now the directory listing works fine for "myserver" but not for "cgi-bin"; maybe that is how it is coded - I don't have a problem here. "sample.html" is retrieved fine too. However, the execution of "cgi_demo.py" is not proper. I get a blank page in the browser; and the console window (which is blank too) appears and disappears. Moreover, on the server's console I get the message
127.0.0.1 - - [29/Nov/2012 12:00:31] "GET /cgi-bin/cgi_demo.py HTTP/1.1" 200 -
127.0.0.1 - - [29/Nov/2012 12:00:31] command: C:\Python33\python.exe -u "D:\python apps\my web server\cgi-bin\cgi_demo.py" ""
127.0.0.1 - - [29/Nov/2012 12:00:32] CGI script exited OK
Please tell me what is wrong! I get the feeling that the output stream of my script is not connected to the server. What am I doing wrong? Don't say that I have to extend CGIHTTPRequestHandler!!
SORRY for the trouble!
Well, it is my fault. 2 things to note:
[1]The console window that appeared and disappeared; it only happens when I use IDLE to execute the server. If the script is already running in a normal windows console then this does not happen. My Feeling was WRONG.
[2]There is an bug/error in my cgi script. After printing the HTTP header; the print statement that I wrote was just "print" instead of actually being "print()".This is so embarrassing! But, even then why didn't the interpreter catch this error?

Resources