2016-08-06 5 views
-2

私は、httpのリクエストがうまく動作している間に、python raw socket.Butを使用してURLにhttpポストを実行しようとしています。 問題は投稿しようとするときです。Python rawソケットを使用したHTTP POSTができません

私のコードエラーがない

import socket 

HOST = 'www.example.com' 

PORT = 80 

DATA = "POST /example/email.php HTTP/1.1\r\n" # send headers 
"HOST: example.com\r\n" 
"Accept: */\r\n*" 
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n" 
"Referer: http://example.com/example/\r\n" 
"action=subscribeme&[email protected]\r\n" #actual post payload data 



def tcp_client(): 
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    client.connect((HOST, PORT)) 
    client.send(DATA) 
    response = client.recv(4096) 
    print response 

if __name__ == '__main__': 
    tcp_client() 

/サーバ側(不正な要求または何か他のもの)から返信が、それは動作していないことのようです。私はPOSTMANとカールでそれらをチェックしたので、他のすべては正しいです。

+0

何らかの応答があるはずです。 200/404/500またはタイムアウト。 – DeepSpace

+0

あなたのデータを印刷して何が得られるかを確認 –

+0

@DeepSpaceエラーはありません。ソケットタイムアウトを5秒に設定しました。さらに、同じhttpパケットがカールと郵便配達員でうまく動作しています –

答えて

1

DATA = ("POST /example/email.php HTTP/1.1\r\n" # send headers 
"HOST: example.com\r\n" 
"Accept: *\r\n" 
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n" 
"Referer: http://example.com/example/\r\n" 
"\r\n" # blank line seperating headers from body 
"action=subscribeme&[email protected]\r\n") #actual post payload data 

でそれを試してみて、あなたは答えを得る必要があります。

import socket 

HOST = 'www.example.com' 

PORT = 80 

DATA = ("POST /example/email.php HTTP/1.1\r\n" # send headers 
"HOST: example.com\r\n" 
"Accept: *\r\n" 
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n" 
"Referer: http://example.com/example/\r\n" 
"\r\n" # blank line seperating headers from body 
"action=subscribeme&[email protected]\r\n") #actual post payload data 


def tcp_client(): 
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    client.connect((HOST, PORT)) 
    client.send(DATA) 
    response = client.recv(4096) 
    print response 

if __name__ == '__main__': 
    tcp_client() 

を使用して

私は明らかに誤りはなく、完全に対応している

HTTP/1.1 411 Length Required  
Content-Type: text/html  
Content-Length: 357  
Connection: close  
Date: Mon, 08 Aug 2016 11:23:16 GMT  
Server: ECSF (ewr/1443) 

<?xml version="1.0" encoding="iso-8859-1"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
     <title>411 - Length Required</title> 
    </head> 
    <body> 
     <h1>411 - Length Required</h1> 
    </body> 
</html> 

を取得します。

DATA = ("POST /example/email.php HTTP/1.1\r\n" # send headers 
"HOST: example.com\r\n" 
"Accept: *\r\n" 
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n" 
"Content-Length: 41\r\n" #fixes error 411 
"Referer: http://example.com/example/\r\n" 
"\r\n" # blank line seperating headers from body 
"action=subscribeme&[email protected]") #actual post payload data 

は、私は通常の404 Not Foundを取得します。

+0

。,,それでも試しましたが成功しません。 –

+0

私はそれをテストした結果、私の結果は答えました – janbrohl

関連する問題