Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:01:24+00:00 2026-06-12T11:01:24+00:00

I found a script on this site for running a simple server via the

  • 0

I found a script on this site for running a simple server via the command line with python.

I added some print lines in because I’d like to print out the GET and POST parameters via the command line for requests, but I can’t seem to get them to show up anywhere.

If I just print our our the s variable (pprint (vars(s))) I end up seeing this:

{'client_address': ('127.0.0.1', 53373),
 'close_connection': 1,
 'command': 'GET',
 'connection': <socket._socketobject object at 0x10b6560c0>,
 'headers': <mimetools.Message instance at 0x10b689ab8>,
 'path': '/favicon.ico',
 'raw_requestline': 'GET /favicon.ico HTTP/1.1\r\n',
 'request': <socket._socketobject object at 0x10b6560c0>,
 'request_version': 'HTTP/1.1',
 'requestline': 'GET /favicon.ico HTTP/1.1',
 'rfile': <socket._fileobject object at 0x10b6538d0>,
 'server': <BaseHTTPServer.HTTPServer instance at 0x10b6893f8>,
 'wfile': <socket._fileobject object at 0x10b6536d0>}

I tried to then use the print command with each of the indices, (pprint (vars(s.connection))) but that’s not working.

Here is the modified script:

#!/usr/bin/python
import time
import BaseHTTPServer
from pprint import pprint

HOST_NAME = 'localhost' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 9000 # Maybe set this to 9000.


class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        def do_HEAD(s):
                s.send_response(200)
                s.send_header("Content-type", "text/html")
                s.end_headers()
        def do_GET(s):
                """Respond to a GET request."""
                s.send_response(200)
                s.send_header("Content-type", "text/html")
                s.end_headers()
                s.wfile.write("<html><head><title>Title goes here.</title></head>")
                s.wfile.write("<body><form action='.' method='POST'><input name='x' value='1' /><input type='submit' /></form><p>This is a test.</p>")
                # If someone went to "http://something.somewhere.net/foo/bar/",
                # then s.path equals "/foo/bar/".
                s.wfile.write("<p>GET: You accessed path: %s</p>" % s.path)
                s.wfile.write("</body></html>")
                pprint (vars(s))
        def do_POST(s):
                """Respond to a POST request."""
                s.send_response(200)
                s.send_header("Content-type", "text/html")
                s.end_headers()
                s.wfile.write("<html><head><title>Title goes here.</title></head>")
                s.wfile.write("<body><p>This is a test.</p>")
                s.wfile.write("<body><form action='.' method='POST'><input type='text' name='xxxxxxxxxxxx' value='0000000000000000000000' /><input type='submit' /></form><p>This is a test.</p>")
                # If someone went to "http://something.somewhere.net/foo/bar/",
                # then s.path equals "/foo/bar/".
                s.wfile.write("<p>POST: You accessed path: %s</p>" % s.path)
                s.wfile.write("</body></html>")
                pprint (vars(s))
                pprint (vars(s.connection))
                pprint (vars(s.headers))
                pprint (vars(s.request))
                pprint (vars(s.rfile))
                pprint (vars(s.server))
                pprint (vars(s.wfile))
                pprint (vars(s.fp))
                """pprint (vars(s.request))"""

if __name__ == '__main__':
        server_class = BaseHTTPServer.HTTPServer
        httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
        print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
        try:
                httpd.serve_forever()
        except KeyboardInterrupt:
                pass
        httpd.server_close()
        print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

How can I print out POST and GET parameters using a simple script?

Desired output via the command line would look something like:

1.0.0.127. - - [03/Oct/2012 16:02:05] "POST / HTTP/1.1" 200 -
foo=1
bar=2
bis=3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:01:25+00:00Added an answer on June 12, 2026 at 11:01 am

    It’s not tremendously obvious, but the handler is using sockets behind the scenes. So you need to read the raw data from the socket, and then interpret it.

    Use the urlparse module.

    • In Python 2, you want urlparse.parse_qs.
    • In Python 3, the library is renamed: you want urllib.parse.parse_qs.

    Import urlparse, and then modify your do_POST method like so:

    def do_POST(s):
            """Respond to a POST request."""
    
            # Extract and print the contents of the POST
            length = int(s.headers['Content-Length'])
            post_data = urlparse.parse_qs(s.rfile.read(length).decode('utf-8'))
            for key, value in post_data.iteritems():
                print "%s=%s" % (key, value)
    
            s.send_response(200)
            s.send_header("Content-type", "text/html")
            s.end_headers()
            ...
    

    Set up a simple test client:

    #!/usr/bin/env python
    
    import urllib
    import urllib2
    
    url = 'http://localhost:9000'
    post_dict = {'foo' : 1,
                 'bar' : 2,
                 'bis' : 3}
    
    params = urllib.urlencode(post_dict)
    post_req = urllib2.Request(url)
    post_req.add_data(params)
    
    response = urllib2.urlopen(post_req)
    response_data = response.read()
    response.close()
    print response_data
    

    Start the server, and then run the client:

    ire@localhost$ python http_server.py 
    Wed Oct  3 21:38:51 2012 Server Starts - localhost:9000
    foo=[u'1']
    bar=[u'2']
    bis=[u'3']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this script attached to a modified index page. This looks like some
Based on some other code I found from this site (This Question here -
I've tried several methods found on this site to get my script to change
I found this script that gives you the username in Windows, but I get
I found this script: <script language=Javascript TYPE=text/javascript> var container = document.getElementById('dl'); var seconds =
Hi i'm building a webapp. To remove the onclick delay i found this script
I've found this login script and I'm trying to implement it into my website
I wanted to use groovy for a little ftp script and found this post
I have found and customized this JQuery script, which displays different content when different
I found this random page script which is in PHP file. I'm a bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.