python - Send raw POST request using Socket -


i trying send raw post request chromedriver server.

here try initiate new session:

import socket  s = socket.socket(     socket.af_inet, socket.sock_stream)  s.connect(("127.0.0.1", 9515))  s.send(b'post /session http/1.1\r\ncontent-type:application/json\r\n{"capabilities": {}, "desiredcapabilities": {}}\r\n\r\n') response = s.recv(4096) print(response) 

output:

b'http/1.1 200 ok\r\ncontent-length:270\r\ncontent-type:application/json; charset=utf-8\r\nconnection:close\r\n\r\n{"sessionid":"b26166c2aac022566917db20260500bb","status":33,"value":{"message":"session not created exception: missing or invalid capabilities\\n  (driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=linux 4.4.0-91-generic x86_64)"}}' 

summary of error: json object sending not getting parsed correctly

when use same json object send requests library, ok:

import requests  params = {         'capabilities': {},         'desiredcapabilities': {}     }   headers = {'content-type': 'application/json'}  url = "http://127.0.0.1:9515"  r = requests.post(url + "/session", json=params)  print("status: " + str(r.status_code)) print("body: " + str(r.content)) 

output:

status: 200 body: b'{"sessionid":"e03189a25d099125a541f3044cb0ee42","status":0,"value":{"acceptsslcerts":true,"applicationcacheenabled":false,"browserconnectionenabled":false,"browsername":"chrome","chrome":{"chromedriverversion":"2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8)","userdatadir":"/tmp/.org.chromium.chromium.lbeqkw"},"cssselectorsenabled":true,"databaseenabled":false,"handlesalerts":true,"hastouchscreen":false,"javascriptenabled":true,"locationcontextenabled":true,"mobileemulationenabled":false,"nativeevents":true,"networkconnectionenabled":false,"pageloadstrategy":"normal","platform":"linux","rotatable":false,"setwindowrect":true,"takesheapsnapshot":true,"takesscreenshot":true,"unexpectedalertbehaviour":"","version":"60.0.3112.90","webstorageenabled":true}}' 

summary of output: json object parsed chromedriver , new session created

do you, guys, have idea why sending raw post request using socket not working expected?

there several issues errors http request:

  • the body of http request should separated head \r\n\r\n.
  • you need content-length field, otherwise remote host not know when body complete.
  • the host field mandatory in http 1.1. (since receive 200 first request, server doesn't insist.)

i've got example work (with apache webserver) using:

s.send(b'post /session http/1.1\r\nhost: 127.0.0.1:9515\r\ncontent-type: application/json\r\ncontent-length: 47\r\n\r\n{"capabilities": {}, "desiredcapabilities": {}}') 

to visually more clear, valid http request looks like

post /session http/1.1 host: 127.0.0.1:9515 content-type: application/json content-length: 47  {"capabilities": {}, "desiredcapabilities": {}} 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -