1) When you send a message through a socket, you have no idea how many blocks will be split. It can be sent immediately; or the first 3 letters can be sent, then the rest of the message; or the message can be divided into 10 parts.
2) Given 1) how should the server know when it received all the pieces sent by the client? For example, suppose a server receives 1 part of a client message. How does the server know if this was the whole message or are there 9 more pieces?
3) I suggest you read the following:
http://docs.python.org/2/howto/sockets.html
(Plus links in the comments)
4) Now, why don't you use python to create an HTTP server?
python3:
import http.server import socketserver PORT = 8000 handler = http.server.SimpleHTTPRequestHandler httpd = socketserver.TCPServer(("", PORT), handler) print("serving at port", PORT) httpd.serve_forever()
python2:
import SimpleHTTPServer import SocketServer PORT = 8000 handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = SocketServer.TCPServer(("", PORT), handler) print "serving at port", PORT httpd.serve_forever()
SimpleHTTPRequestHandler serves files from the server program directory and below, matching the request URL with the created directory structure. If you request "/", the server will serve the index.html file from the same directory in which the server is located. Below is an example client socket for python 3 (python 2 example below):
import socket import sys try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error: print('Failed to create socket') sys.exit() print('Socket Created')
In python 3 you have to use byte strings with sockets, otherwise you will get scary:
TypeError: 'str' does not support the buffer interface
Here it is in python 2.x:
import socket import sys try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error: print 'Failed to create socket' sys.exit() print('Socket Created')
Note that the GET request header tells the server that HTTP 1.1 will be the protocol, that is, the rules governing the conversation. And, as the RFC describes for HTTP 1.1, the request must have two sequences "\ r \ n". Thus, the server searches for the second second sequence "\ r \ n". If you remove one of the sequences "\ r \ n" from the request, the client will hang on recv () because the server is still waiting for more data because the server did not read this second '\ r \ n' sequence.
Also note that you will send data as bytes (in python 3), so there will be no automatic conversions "\ n", and the server will wait for the sequence "\ r \ n".