0

I have a string URL that I want to send or "post" to a target IP.

I've tried using the requests library but I keep getting errors.

import requests
data = {'http://192.168.0.1/mobile/write.fcgi?
serverId=1'}
r = requests.post('http://192.168.0.1', data=data)

The error says a bytes-like object is required, not 'str'.

How can I send this string to the IP 192.168.0.1 (local host): http://192.168.0.1/mobile/write.fcgi?serverId=1

Jack Hudgins
  • 93
  • 2
  • 7
  • You need to encode it. – Jim Wright Feb 14 '18 at 16:25
  • 1
    Possible duplicate of [Best way to convert string to bytes in Python 3?](https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) – Jim Wright Feb 14 '18 at 16:25
  • 3
    Your data is a set (should be dict or str) – t.m.adam Feb 14 '18 at 16:29
  • 1
    Going out on a limb here without enough info, but I suspect you want `data = {'serverId': '1'}` / `r = requests.get('http://192.168.0.1/mobile/write.fcgi', params=data)` – Robᵩ Feb 14 '18 at 16:35
  • "Can the controller send to a defined target IP address the following string whenever a fence is triggered: “http:///mobile/write.fcgi?serverId=1&pin=82&status=1” " I have the code setup to read when an alarm goes off, then when alarm = True I need to send the above string (url) to a target IP. – Jack Hudgins Feb 14 '18 at 16:42

1 Answers1

0

Simply adding a b before the string in the data variable should fix this.

import requests
data = {b'http://192.168.0.1/mobile/write.fcgi?serverId=1'}
r = requests.post('http://192.168.0.1', data=data)

Edit: Seeing the comments I might be wrong, but testing the code with a different website gave a response (screenshot)

Edit 2: Checked the r.content and it gives this, which is a 405 error (screenshot). A 405 error simply means that the script doesn't have the necessary permissions (on the target website) to use the post function.

Helen
  • 185
  • 11