Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: run SimpleHTTPServer and make request to it in a script

I would like to write Python script that would:

  1. Start SimpleHTTPServer on some port
  2. Make a request to the server and send some data via POST
  3. Run script via shell in interactive mode and interact with it

Right now the code looks like this:

import SimpleHTTPServer
import SocketServer
import urllib
import urllib2

# Variables
URL = 'localhost:8000'
PORT = 8000

# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()

# Getting HTML from the target page
values = {
    'name': 'Thomas Anderson',
    'location': 'unknown'
}
data = urlilib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
html = response.read()

The problem is that once I run my script

python -i foo.py

It prints serving at port 8000 and then freezes. I bet this is something trivial for Python gurus here, but help would be appreciated.

like image 336
0leg Avatar asked Jan 21 '16 19:01

0leg


People also ask

How do I transfer files using SimpleHTTPServer?

Go to the directory whose file you want to share by using cd (change directory) command. Go to the directory with the file you want to share using cd on *nix or MacOS systems or CD for Windows. Start your HTTP server with either python -m SimpleHTTPServer or python3 -m http. server.

What does Python SimpleHTTPServer do?

The SimpleHTTPServer module is a Python module that enables a developer to lay the foundation for developing a web server. However, as sysadmins, we can use the module to serve files from a directory. The module loads and serves any files within the directory on port 8000 by default.


1 Answers

Run the server as a different process, that will allow you to run the rest of your script.

I would also rather use requests than urllib.

import SocketServer
import SimpleHTTPServer

import requests
import multiprocessing

# Variables
PORT = 8000
URL = 'localhost:{port}'.format(port=PORT)

# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Serving at port", PORT

# start the server as a separate process
server_process = multiprocessing.Process(target=httpd.serve_forever)
server_process.daemon = True
server_process.start()

# Getting HTML from the target page
values = {
    'name': 'Thomas Anderson',
    'location': 'unknown'
}

r = requests.post(URL, data=values)
r.text

# stop the server
server_process.terminate()
like image 57
Dušan Maďar Avatar answered Sep 28 '22 06:09

Dušan Maďar