3

I have 2 networks on my computer (Wifi network for internet, and local network based on wired cable) with specific mask and IP (this configuration works).

I have a Python HTTP server (on my computer) for the local network:

or simply

>>> python server.py
Serving on localhost:8000

You can use this to test GET and POST methods.

"""

import SimpleHTTPServer
import SocketServer
import logging
import cgi

import sys


if len(sys.argv) > 2:
    PORT = int(sys.argv[2])
    I = sys.argv[1]
elif len(sys.argv) > 1:
    PORT = int(sys.argv[1])
    I = ""
else:
    PORT = 8000
    I = ""


class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        print "======= GET STARTED ======="
        logging.warning("======= GET STARTED =======")
        logging.warning(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

    def do_POST(self):
        print "======= POST STARTED ======="
        logging.warning("======= POST STARTED =======")
        logging.warning(self.headers)
        form = cgi.FieldStorage(
            fp=self.rfile,
            headers=self.headers,
            environ={'REQUEST_METHOD':'POST',
                     'CONTENT_TYPE':self.headers['Content-Type'],
                     })
        logging.warning("======= POST VALUES =======")
        for item in form.list:
            logging.warning(item)
        logging.warning("\n")
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = ServerHandler

httpd = SocketServer.TCPServer(("10.0.0.2", PORT), Handler)

print "@rochacbruno Python http server version 0.1 (for testing purposes only)"
print "Serving at: http://%(interface)s:%(port)s" % dict(interface=I or "localhost", port=PORT)
httpd.serve_forever()  

I just want to try if this server works: from my Google CHrome, I set this URL: 10.0.0.2/?method=1234

And no trace appears on the Python shell...

So, my question is: how can I test this Python server from my computer in first (to be sure it works) ?

Termininja
  • 6,620
  • 12
  • 48
  • 49
Anthony
  • 325
  • 2
  • 5
  • 15
  • I've ran your code... it epicly fails and returns me **socket.error: [Errno 10049] The requested address is not valid in its context** whether I change the port from 8000 to 888 or not. – ZF007 Dec 10 '17 at 22:06
  • see my answer that solves it. – ZF007 Dec 11 '17 at 16:29

2 Answers2

0

Ok guys, I found the problem: it was the port number; I changed it to 888 for example and it works now :-)

Anthony

Anthony
  • 325
  • 2
  • 5
  • 15
0

Your code throws in 2017 the following error:

socket.error: [Errno 10049] The requested address is not valid in its context

The error is caused by the following codeline:

httpd = SocketServer.TCPServer(("10.0.0.2", PORT), Handler)

when IP "10.0.0.2" is changed into "127.0.0.1" it works again regardless of your port update 8000 -> 888. Enjoy!

ZF007
  • 3,708
  • 8
  • 29
  • 48