how to send image to server by post to http?

So I replaced it with urequests.py from Wipy link, I get following error;

OSError: [Errno 107] ENOTCONN

OpenMV Terminal:
Traceback (most recent call last):
File “”, line 47, in
File “urequests.py”, line 107, in post
File “urequests.py”, line 124, in urlopen
File “urequests.py”, line 27, in init
OSError: [Errno 107] ENOTCONN

So it opens urequests.py in OpenMV and stops at line 27 as shown in abovementioned traceback.

This is line 27 in urequests.py; s.settimeout(5)

Thank you for your patience.

Would you explain more what I should do to fix the problem with the WLAN interface?
I mean where can I find the issue in the current lucien2k code?
Also how to solve the OSError: [Errno 107] ENOTCONN.

Thank you

You need to init the wifi module first. The library just uses the socket interface. It does not setup the wifi module. See our wifi examples. You have to actually turn on the wifi part. Once that’s done MicroPython offers socket services to code which the library uses.

1 Like

When I add the urequests.post at the end I get the OSError: [Errno 107] ENOTCONN for s.settimeout(3) in line 28, am I doing the init correctly?

#Details:

Traceback (most recent call last):
File “”, line 39, in
File “urequests.py”, line 108, in post
File “urequests.py”, line 125, in urlopen
File “urequests.py”, line 28, in init
#OSError: [Errno 107] ENOTCONN

#My Code:
import network, usocket, ustruct, utime, pyb, os, urequests, urllib

SSID=‘’ # Network SSID
KEY=‘’ # Network key
PORT = 80
HOST = “www.x.com
print(“Trying to connect… (may take a while)…”)
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
print(wlan.ifconfig())
addr = usocket.getaddrinfo(HOST, PORT)[0][4]
print(addr)
client = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
client.connect(addr)
client.settimeout(10.0)
url = “http://www.x.com/upload.php/
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0’,
}
file = {‘file’: open(‘image.png’, ‘rb’)} #open file to post
r = urequests.post(url, data=file, headers=headers) #post request of file to url
print(r.status_code) #status code 200 means success

Thanks

You need to add your network in the SSID and KEY fields…

Please use code option formatting to post so I can read your code.

Oh, I just removed ssid and pass online, it has it in IDE, here is the code;

My Code:


import network, usocket, ustruct, utime, pyb, os, urequests, urllib

SSID=’x’ # Network SSID
KEY=’x’ # Network key
PORT = 80
HOST = “www.x.com”
print(“Trying to connect… (may take a while)…”)
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
print(wlan.ifconfig())
addr = usocket.getaddrinfo(HOST, PORT)[0][4]
print(addr)
client = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
client.connect(addr)
client.settimeout(10.0)
url = “http://www.x.com/upload.php/”
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:55.0) Gecko/20100101 Firefox/55.0’,
}
file = {‘file’: open(‘image.png’, ‘rb’)} #open file to post
r = urequests.post(url, data=file, headers=headers) #post request of file to url
print(r.status_code) #status code 200 means success

What prints out? Does print(addr) return valid info that you actually got an IP address?

Yes, it returns a valid address, the IP of my website:

First the local IP address: (‘192.168.0.105’, ‘255.255.255.0’, ‘192.168.0.1’, ‘192.168.0.1’)
Then IP of my website and conventional 80 port number: (‘IP of my website’, 80)

Then I get errors:
Traceback (most recent call last):
File “”, line 39, in
File “urequests.py”, line 108, in post
File “urequests.py”, line 125, in urlopen
File “urequests.py”, line 28, in init
OSError: [Errno 107] ENOTCONN
MicroPython: e439f36 OpenMV: v4.0.1 HAL: v1.9.0 BOARD: OPENMV4P-STM32H743
Type “help()” for more information.

If you wanna test if you can upload an image to my website, I can email you my website address as its upload.php is working with the Python request library.

Thanks

K, it seems like this line is having problems: wipy-urllib/urequests.py at master · lucien2k/wipy-urllib · GitHub

I’d add some print statements and see what happens.

The error is that the socket was not able to connect.

1 Like

Is it because he “imports socket” as is? In OpenMV we have usocket not socket.
So I also added the following line below the above statement on urequests.py but it did not help;
import usocket as socket

@iabdalkader - Can you help? Too busy right now to provide more details.

1 Like

Thanks Kwabena and Ibrahim,

If you need an upload website my website currently works receiving requests from my computer, I mentioned the Python code in this thread. So if you provide an email, I can share my website links;

  • To upload the photo from OpenMV

  • Use the gallery link that you can see if the photo on OpenMV is uploaded there

You should be using this one:

It may need some modifications to work with our module.

1 Like

I think it should be json=file not data, or send the file object directly or (file.read())

Thanks

r = urequests.post(url, json=(file.read()), headers=headers) 
AttributeError: 'dict' object has no attribute 'read'
r = urequests.post(url, json=file, headers=headers)
  File "urequests.py", line 84;  l = s.readline()
AttributeError: 'socket' object has no attribute 'readline'

“or send the file object directly” Not sure how to implement this

Btw for all these, first I had to modify urequest module as follows on line 55 to remove the 2 positional argument error mentioned recently;
ai = usocket.getaddrinfo(host, port) #, 0, usocket.SOCK_STREAM)

The image should be posted and it’s trying to read back the HTTP response. The socket object doesn’t have a readline function you’ll need to add that yourself, just write a function that reads 1 byte in a loop and append it to a string (or bytearray) until you get a newline char ‘\n’ then return the string.

Note: Please put some more effort into fixing this, you’re basically copy&pasting errors for us to explain/fix for you. If you read the library you will understand what it’s doing and why it is failing, if you’re not a programmer I can’t help you with that.

Thanks for your help, you are right, I should work on it more!
I was focused on remembering the OOP again today with a Coursera tutorial to understand the library better.
I Will put more effort into fixing the library…

I’ll try to set up a web server so I can test this and fix it to include it in the built-in modules for the next firmware, but it may take some time.

BTW this is actually more compatible with stm32 sockets, I don’t know why you couldn’t use it, it should be working (urequests.post(…)), it also supports auth and cookies, we may actually use this one instead.

1 Like

I can use a local server for testing, but it will still take time to test and fix one of those modules, best if you try to get it working for now.

1 Like