Конвертировать код библиотеки Python http в Socket без библиотеки http - PullRequest
0 голосов
/ 20 октября 2019

Я пытался преобразовать приведенный ниже код в Socket без библиотеки http. Ответ на запрос отличается от использования библиотеки http. Пожалуйста, помогите мне преобразовать код в сокет. В веб-сервере запрос должен быть таким: http://192.168.1.15/api/v2.0.0/status

import http.client

conn = http.client.HTTPConnection("192,168,1,15")

payload = ""

headers = {
    'Content-Type': "application/json",
    'Authorization': "Basic YWRtaW46OGM2OTc2ZTViNTQxMDQxNWJkZTkwOGJkNGRlZTE1ZGZiMTY3YTljODczZmM0YmI4YTgxZjZmMmFiNDQ4YTkxOA=="
    }

conn.request("GET", "api,v2.0.0,status", payload, headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

Ответ должен быть в Json:

{
    "allowed_methods": null,
    "battery_percentage": 100,
    "battery_time_remaining": 136800,
    "distance_to_next_target": 0,
    "errors": [],
    "footprint": "[[0.506,-0.32],[0.506,0.32],[-0.454,0.32],[-0.454,-0.32]]",
    "joystick_low_speed_mode_enabled": false,
    "joystick_web_session_id": "",
    "map_id": "36c89a77-57a7-11e9-b33b-94c691a7386a",
    "mission_queue_id": null,
    "mission_queue_url": null,
    "mission_text": "Starting...",
    "mode_id": 7,
    "mode_key_state": "idle",
    "mode_text": "Mission",
    "moved": 225.17,
    "position": {
        "orientation": -139.83770751953125,
        "x": 14.193017959594727,
        "y": 31.63810920715332
    },

1 Ответ

0 голосов
/ 22 октября 2019
Here i attached the code what i tried, 

import socket

server = ("192.168.1.15", 80)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(server)
msg  = "GET / HTTP/1.1\r\n"
msg += "Host: 192.168.1.15:80\r\n"
msg += "Path: /api/v2.0.0/status\r\n"
msg += "Content-Type: application/json\r\n"
msg += "Connection: close\r\n"
msg += "\r\n"
sock.sendall(msg.encode())
data = sock.recv(50000)
sock.close()
print(data.decode())

Server Response in html format, expected json format which shown in above,

HTTP/1.1 200 OK
Date: Tue, 22 Oct 2019 13:36:51 GMT
Server: Apache/2.4.18 (Ubuntu)
Set-Cookie: PHPSESSID=rktgr2p1mu3qnu1he1msjij7a5; expires=Thu, 24-Oct-2019 
13:36:51 GMT; Max-Age=172800; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Mir-Page-Id: 6ef0b1a
Set-Cookie: mir-auth-token=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max- 
Age=0; path=/
Set-Cookie: PHPSESSID=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max- 
Age=0; path=/
Set-Cookie: mir_user_id=0; expires=Tue, 22-Oct-2019 13:35:51 GMT; Max-Age=0; 
path=/
Set-Cookie: mir_user_shortcode=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; 
Max-Age=0; path=/
Vary: Accept-Encoding
 Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

2007
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="Content-Type" content="text/html">
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="author" content="Mobile Industrial Robots ApS">
<meta name="owner" content="Mobile Industrial Robots ApS">
<meta name="viewport" content="width=device-width, height=device-height, 
initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="mobile-web-app-capable" content="yes">

The reason I working without http library because I wanna use socket code in my 
PLC Programmming, In PLC don't have http library to communicate with this 
server,PLC already have socket communication.

Hope Can Support me,Thanks in Advance.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...