Problems Returning an HTTP 200 Ok Message

Guy Davidson GDavidson7 at
Thu Jul 10 20:50:14 CEST 2008

Hi Folks,

I'm having some issues with an small socket based server I'm writing,
and I was hoping I could get some help.

My code (attached below) us supposed to read an HTTP Post message
coming from a power meter, parse it, and return a proper HTTP 200 Ok
message. The problem is that the socket fails to send the entire
message as one message, creating a fragmented message which the power
meter then fails to read and accept.

Is there any way to force the socket to send the entire message at
once? Am I doing anything wrong? Is there an easier way to implement
this functionality?


Guy Davidson


# server application to read data from power meter
# listens on port 80
# receives HTTP POST messages

# author: Maria Kazandjieva <mariakaz at>, Guy Davidson
<gudyav2002 at>
# updated: July 7th, 2008: staring to add database writing

import time
import datetime #datetime for response message
from socket import * #socket module
import DataPacket #local file with data packet class
import library
import MySQLdb

#formats a proper response message

myHost = ""
myPort = 80

#response = "HTTP/1.1 200 OK\r\nDate: Fri, 20 June 2008 20:40:34 GMT\r
\nServer:SING\r\nX-Powered-By: maria\r\nContent-Length: 7\r
\nConnection:close\r\nContent-Type: text/html\r\n\r\n[0!:20]\n"

#initializing the socket:
s = socket(AF_INET, SOCK_STREAM)        # create a TCP socket
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # allow port reuse? I hope
s.bind((myHost, myPort))                # bind it to the server port
s.listen(50)                            # allow 50 simultaneous
pending connections
print('Socket initialized')

#intializing the database
db = MySQLdb.connect(host='localhost', user='powernet',
passwd='jplicks', db='test')
cursor = db.cursor()
print('Database cursor intialized')

while 1:
	print('In while 1')
	connection, address = s.accept()        # connection is a new socket
	print('Accepted a connection')

	# TODO:
	# verify connection is from powernet so we don't get random
connections to port 80

	# best way to get the data would be:
	# read until we get all the headers
	# parse Content header to get length of data
	# read for that many additional bytes

	while 1:
		print('In while 1 2')
		data = connection.recv(4096)    # receive up to 4K bytes
		print('First set of data receieved:')
		if data:
			print data
			#connection.send(response)      #send response; OK 200 HTTP message
		if data:
			# second read should recieve the rest of the packet
			# finding how long the content is
			length = data.find('Content-Length: ')
			print('Index of "Content-Length" = ' + str(length))
			length = int(data[length+16:length+18])
			# adding the bytes from the "Content-Type section:"
			length += 51

			data += connection.recv(length)
			print('Second set of data received:')

			if data:
				#print('Found data:')
				#print data #print the string data
				packet = DataPacket.DataPacket(data) #use the string data to
create a DataPacket object
				print(packet) #print the contents of the object, I hope :)

				#we have a packet, let's write it to the mysql DB

				#connection.send(response)      #send response; OK 200 HTTP
				print('Sending response')
				response = library.response_message() #get the response message
				totalsent = 0 #initialize a variable to track how much we've sent
so far
				while totalsent < len(response): #while we haven't sent everything
					sent = connection.send(response[totalsent:]) #send whatever we
can starting where we stopped
					totalsent += sent #increment the count of how much we've sent.

				print('No data')

			print('No data')

More information about the Python-list mailing list