Cgi Return Http Error Code
Contents |
Gateway Interface 3.7 Status Codes Status codes are used by the HTTP protocol to communicate the status of a request. For example, if a document does not exist, the server returns a "404"
Perl Return Http Status Code
status code to the browser. If a document has been moved, a "301" status code python cgi status code is returned. CGI programs can send status information as part of a virtual document. Here's an arbitrary example that returns success if the perl cgi header remote host name is bu.edu, and failure otherwise: #!/usr/local/bin/perl $remote_host = $ENV{'REMOTE_HOST'}; print "Content-type: text/plain", "\n"; if ($remote_host eq "bu.edu") { print "Status: 200 OK", "\n\n"; print "Great! You are from Boston University!", "\n"; } else { print
Html Status Codes
"Status: 400 Bad Request", "\n\n"; print "Sorry! You need to access this from Boston University!", "\n"; } exit (0); The Status header consists of a three-digit numerical status code, followed by a string representing the code. A status value of 200 indicates success, while a value of 400 constitutes a bad request. In addition to these two, there are numerous other status codes you can use for a variety of situations, ranging from an unauthorized or forbidden
Python Cgi Example
request to internal system errors. Table 3.3 shows a list of some of commonly used status codes. Table 3.3: HTTP Status Codes Status Code Message 200 Success 204 No Response 301 Document Moved 401 Unauthorized 403 Forbidden 404 Not Found 500 Internal Server Error 501 Not Implemented For a complete listing of status codes, see: http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html Unfortunately, most browsers do not support all of them. The "No Response" Code One status code that deserves special attention is status code 204, which produces a "no response." In other words, the browser will not load a new page if your CGI program returns a status code of 204: #!/usr/local/bin/perl print "Content-type: text/plain", "\n"; print "Status: 204 No Response", "\n\n"; print "You should not see this message. If you do, your browser does", "\n"; print "not implement status codes correctly.", "\n"; exit (0); The "no response" status code can be used when dealing with forms or imagemaps. For example, if the user enters an invalid value in one of the fields in a form or clicks in an unassigned section of an imagemap, you can return this status code to instruct the client to not load a new page. The "Expires" and "Pragma" Headers Complete (Non-Parsed) Headers Back to: CGI Programming on the World Wide Web oreilly.com Home | O'Reilly Bookstores | How to Order | O'Reilly Contacts International |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss python requests the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each http://www.oreilly.com/openbook/cgi/ch03_07.html other. Join them; it only takes a minute: Sign up Python CGI returning an http status code, such as 403? up vote 14 down vote favorite 1 How can my python cgi return a specific http status code, such as 403 or 418? I tried the obvious (print "Status:403 Forbidden") but it doesn't work. python http cgi http-status-codes share|improve http://stackoverflow.com/questions/1411867/python-cgi-returning-an-http-status-code-such-as-403 this question asked Sep 11 '09 at 16:09 cfischer 8,4761891165 add a comment| 2 Answers 2 active oldest votes up vote 16 down vote accepted print 'Status: 403 Forbidden' print Works for me. You do need the second print though, as you need a double-newline to end the HTTP response headers. Otherwise your web server may complain you aren't sending it a complete set of headers. sys.stdout('Status: 403 Forbidden\r\n\r\n') may be technically more correct, according to RFC (assuming that your CGI script isn't running in text mode on Windows). However both line endings seem to work everywhere. share|improve this answer answered Sep 11 '09 at 16:27 bobince 364k75479687 add a comment| up vote 1 down vote I guess, you're looking for send_error. It would be located in http.server in py3k. share|improve this answer answered Sep 11 '09 at 16:14 SilentGhost 125k33218236 I'm using python 2.5. Will that work? –cfischer Sep 11 '09 at 16:25 I don't see why it shouldn't. –SilentGhost Sep 11 '09 at 16:25 On
Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://serverfault.com/questions/121735/how-to-return-404-from-apache2-cgi-program or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question _ Server http://www.perlmonks.org/?node_id=263134 Fault is a question and answer site for system and network administrators. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to return 404 from Apache2 CGI program up vote 4 down vote favorite 1 I'm using mod_rewrite to redirect status code all incoming requests to a CGI application. I now need to have the application return a 404 if the requested file isn't found. How can I go about this from my program? The first line sent is the content-type while it's the line before that that usually indicates the status (200/404/500, etc). apache-2.2 cgi share|improve this question asked Mar 11 '10 at 23:53 user11888 3915 add a comment| 1 Answer 1 active oldest votes up vote 8 down vote accepted Use cgi return http the Status HTTP header. An example in Perl: #!/usr/bin/perl use strict; use warnings; print "Status: 404 Not Found\r\n"; print "Content-Type: text/html\r\n\r\n"; print "
404 File not found!
"; When using Perl (and other languages) however, excellent modules like CGI and CGI::Simple exist. share|improve this answer answered Mar 12 '10 at 0:16 Mikael S 1,08286 Dang, is it really that easy? The CGI tutorial I was following just had me sending the Content-Type line, I had assumed that the Status line was being sent by Apache. I will give this a try tomorrow, thanks! –user11888 Mar 12 '10 at 1:50 Alright, that did the trick. –user11888 Mar 12 '10 at 19:13 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged apache-2.2 cgi or ask your own question. asked 6 years ago viewed 4055 times active 6 years ago Related 1Apache2 cgi's crash on odbc db access (but run fine from shell)1How to configure Apache so all requests go to single CGI file2Is there another way to run Apache2 securely for end users without using CGI mode?1How to convert php5 cgi to module?0Tutorials Poetry RecentThreads NewestNodes Donate What'sNew on Jun 04, 2003 at 19:51UTC ( #263134=perlquestion: print w/replies, xml ) Need Help?? footpad has asked for the wisdom of the Perl Monks concerning the following question: The apprentice tries to dust off and improve some unused skills... I'm creating a 404 ErrorDocument handler to perform redirects based on file name changes on my site's source files. Here's an abbreviated version of what's currently working: #!/usr/bin/perl -wT use strict; use CGI qw(:standard); $| = 1; my $cgi = new CGI; my $dir = '/my/httpd/path'; # spoofed my $old = $ENV{ 'REDIRECT_URL' } || ''; my $new = ''; if ( $old =~ /(.*?)\.foo/i ) { $new = "$1.bar"; if ( -e "$dir$new" ) { print redirect( $new ); exit; } } else { doStandard404(); } [download] My petition is, "Is there a way to set the status code of the redirection?" (The idea, as you might expect, is to return a 301 indicating that the document has moved permanently, so the search engines will notice the change and update their databases accordingly) Alternatively, does CGI.pm already return an appropriate error code? I've checked the Friendly Docs, but didn't see anything. Also, a few google searches didn't turn up anything that seemed relevant, though may be due to my lack of tilly-fu.) Details, if they're relevant, include Apache 2.0, Perl 5.6.1, CGI.pm 2.93 Thanks in advance... --fComment on CGI Redirect with StatusDownload Code Replies are listed 'Best First'. Re: CGI Redirect with Status by Ovid (Cardinal) on Jun 04, 2003 at 20:40UTC I've never been quite sure why this is, but CGI.pm hard-codes the redirection value: sub redirect { my($self,@p) = self_or_default(@_); my($url,$target,$cookie,$nph,@other) = rearrange([[LOCATION,URI,UR +L],TARGET,['COOKIE','COOKIES'],NPH],@p); $url ||= $self->self_url; my(@o); foreach (@other) { tr/\"//d; push(@o,split("=",$_,2)); } unshift(@o, '-Status' => '302 Moved', '-Location'=> $url, '-n