openmv/scripts/examples/14-WiFi-Shield/http_client.py
2019-10-25 23:18:49 +02:00

38 lines
778 B
Python

# Simple HTTP client example.
import network, usocket
# AP info
SSID='' # Network SSID
KEY='' # Network key
PORT = 80
HOST = "www.google.com"
# Init wlan module and connect to network
print("Trying to connect... (may take a while)...")
wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)
# We should have a valid IP now via DHCP
print(wlan.ifconfig())
# Get addr info via DNS
addr = usocket.getaddrinfo(HOST, PORT)[0][4]
print(addr)
# Create a new socket and connect to addr
client = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
client.connect(addr)
# Set timeout
client.settimeout(3.0)
# Send HTTP request and recv response
client.send("GET / HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST))
print(client.recv(1024))
# Close socket
client.close()