0

The thing is the info I'm sending (Alexis Ahumada 1990) never stays on the server log (www.inf.utfsm.cl/~mvaras/tarea1.log) I'd want to know what I'm doing wrong.

#!/usr/bin/env python


import socket
import sys

HOST = 'www.inf.utfsm.cl'
GET = '/~mvaras/tarea1.php'
UA = 'tarea1'
PORT = 80

try:
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
  sys.stderr.write("[ERROR] %s\n" % msg[1])
  sys.exit(1)

try:
  sock.connect((HOST, PORT))
except socket.error, msg:
  sys.stderr.write("[ERROR] %s\n" % msg[1])
  sys.exit(2)

sock.send("GET %s HTTP/1.0\r\nHost: %s\r\n\r\nUser-Agent: %s\r\n\r\n" % (GET, HOST, UA))
sock.send("POST /~mvaras/tarea1.php HTTP/1.0 User-Agent:tarea1 Nombre=Alexis+Ahumada&rut=1990")


data = sock.recv(1024)
string = ""
while len(data):
  string = string + data
  data = sock.recv(1024)

    print string

    sys.exit(0)

1 Answers1

1

You cannot just POST to a server, you have to POST to an URL. In your GET request you specified /~mvaras/tarea1.php as your URL but you didn't to that to the POST.

Besides, you are not using HTTP 1.1 properly because you did not include a Host header as specified on the section 14 of the RFC

A client MUST include a Host header field in all HTTP/1.1 request messages . If the requested URI does not include an Internet host name for the service being requested, then the Host header field MUST be given with an empty value.

On a side note please keep in mind that using HTTP over sockets is something you do only if you really need. If you just want to get some HTTP data, use implementations of higher level.

FBidu
  • 972
  • 9
  • 21
  • I'm so sorry, i uploaded an old version of the code now i edited it. The reason for what I'm using sockets it's because a requirement. – Nando Ahumada Aug 31 '15 at 02:56
  • Okay! Are you using an Apache web server? – FBidu Aug 31 '15 at 02:57
  • Red-Hat it seems i'll give you the results i get with that code HTTP/1.1 200 OK Date: Mon, 31 Aug 2015 02:38:42 GMT Server: Apache/2.2.15 (CentOS) X-Powered-By: PHP/5.3.3 Content-Length: 378 Content-Type: text/html; charset=UTF-8 Connection: close Estimado alumno(a) en el contexto de la Tarea1 de la asignatura Redes de Computadores debes enviar tu nombre y rut – Nando Ahumada Aug 31 '15 at 02:59
  • It looks like Apache is unable to log POST requests at all! http://stackoverflow.com/questions/989967/best-way-to-log-post-data-in-apache – FBidu Aug 31 '15 at 03:00
  • That file (tarea1.log) is being written by your PHP code? – FBidu Aug 31 '15 at 03:02
  • So it probably won't log POST data even if your code works properly – FBidu Aug 31 '15 at 03:06
  • that is i was thinking, it seems the code has no problem at all. Could it be that the problem resides on the Web-Server?. – Nando Ahumada Aug 31 '15 at 03:08
  • I sent a POST request from my computer using HTTPRequester and it did not showed up in the log as well – FBidu Aug 31 '15 at 03:09
  • Well, web servers are not meant to log POST data because this data can get quite big. If you need to work with that data, do it from your tarea1.php – FBidu Aug 31 '15 at 03:10
  • Now since Post is out of the question, it's possible to send the info using Get? – Nando Ahumada Aug 31 '15 at 03:10
  • But then again, I'd use the PHP for processing the data using the `$_POST["Nombre"]` and `$_POST["rut"]` variables to access the POSTed data – FBidu Aug 31 '15 at 03:15
  • De nada. Buenas noches :) – FBidu Aug 31 '15 at 03:18