efl.ecore_con.Url
Class¶efl.ecore_con.
Url
(url, custom_request=None, **kargs)¶New in version 1.17.
Create an Url
object with ecore_con.Url(‘myurl’)
Register object callbacks using on_complete_event_add()
,
on_progress_event_add()
and on_data_event_add()
to
receive the response, e.g. for HTTP/FTP downloads.
Perform the operation with get()
, head()
and post()
If it’s necessary use the url
property. to change the object url.
Note
It is good practice to reuse Url
objects wherever possible,
but bear in mind that each one can only perform one operation at a
time. You need to wait for the complete event before re-using or
destroying the object.
Warning
It is really important to call the delete()
method as soon
as you have finished with your object, as it automatically remove all
the registered events for you, that will otherwise continue to use
resources.
Basic usage examples:
# HTTP GET
u = ecore.Url("http://www.google.com")
u.get()
# HTTP POST
u = ecore.Url('https://httpbin.org/post')
u.post(b'my data to post', 'text/txt')
# FTP download
u = ecore.Url("ftp://ftp.example.com/pub/myfile")
u.get()
# FTP upload as ftp://ftp.example.com/file
u = ecore.Url("ftp://ftp.example.com")
u.ftp_upload("/tmp/file", "user", "pass", None)
# FTP upload as ftp://ftp.example.com/dir/file
u = ecore.Url("ftp://ftp.example.com")
u.ftp_upload("/tmp/file", "user", "pass", "dir")
To actually make something usefull with your request you will need to
connect the EventUrlComplete
, EventUrlProgress
and
EventUrlData
events using the on_complete_event_add()
and
friends functions.
A more complete example:
from efl import ecore
def on_data(event):
print("data: " + str(event.data[:80]))
# do something here with the received data
def on_progress(event):
# print(event)
print("received %d on a total of %d bytes" % (
event.down_now, event.down_total))
def on_complete(event):
# print(event)
print("http result: %d" % event.status)
print("Total received bytes: %d" % event.url.received_bytes)
u.delete() # don't forget to delete !!
u = ecore.Url('http://www.google.com', verbose=False)
u.on_data_event_add(on_data)
u.on_progress_event_add(on_progress)
u.on_complete_event_add(on_complete)
u.get()
ecore.main_loop_begin()
If you need to save the received data to a file use the fd
property, as:
fd = open('/tmp/tmpMxBtta', 'w')
u = ecore.Url('http://example.com', fd=fd.fileno())
u.get()
See also
If you just need to download a file please consider using the
simpler efl.ecore.FileDownload
class instead.
See also
The ecore module level functions url_pipeline_set()
and
url_pipeline_get()
to enable HTTP 1.1 pipelining.
url (string) – URL that will receive requests.
custom_request (string) – Custom request (e.g. GET, POST, HEAD, PUT, HEAD, SUBSCRIBE and other obscure HTTP requests)
**kwargs – All the remaining keyword arguments are interpreted as properties of the instance
New in version 1.17.
efl.ecore_con.
EventUrlComplete
¶This event notifies the operation is completed.
url (Url
): the object that generate the event
status(int): HTTP status code of the operation (200, 404, 401, etc.)
efl.ecore_con.
EventUrlProgress
¶This event notifies the progress of the current operation.
url (Url
): the object that generate the event
down_total(double): total size of the downloading data (in bytes)
down_now(double): current size of the downloading data (in bytes)
up_total(double): total size of the uploading data (in bytes)
up_now(double): current size of the uploading data (in bytes)
efl.ecore_con.
EventUrlData
¶This event hold the data while the are received.
Note
The data attribute is a raw series of bytes, map to str
in python2
and bytes
in python3.
url (Url
): the object that generate the event
size(int): the size of the current received data (in bytes)
data(bytes): the data received on this event