5

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.

2 Answers 2

4

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()
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, this is what I was looking for, but couldn't get due to python-experience limit.
One thing I've noticed, is an error while trying to execute this: socket.error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permit ted
This is most likely due to the fact, that you are not shutting down your http server, at least in your example. Please see my answer for more detail.
Do you get this error when you try to restart the script? So yes, as @NarūnasK suggests, it may be the case that the server is still running and therefore the port 8000 is taken. The answer was edited.
4

Alternatively run it in the separate thread:

import SimpleHTTPServer
import SocketServer
import urllib2
from threading import Thread
from datetime import time

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

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

simple_sever_T = Thread(target=simple_sever, name='simple_sever')
simple_sever_T.daemon = True
simple_sever_T.start()

while not simple_sever_T.is_alive():
    time.sleep(1)

# Getting test file
req = urllib2.Request('http://localhost:%s/test_file' % PORT)
response = urllib2.urlopen(req)
print response.read()
httpd.shutdown()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.