Socket module
Socket module
I am not sure if this topic has been discussed earlier, but I could not find any.
I have developed a module: modsock
The implementation is based on the unix modsocket module but now it is available on the stmhal port using the Adafruit CC3000 module driver code.
I am new and getting up to speed on the MP C side of the code. So my implementation might have some issues due to the fact that not all the APIs/DEFINES are clear to me when using the Python/C interface.
NOTE: there were some issues using send and recv, these required some modifications to the buffer size found in cc3000_common.h
I am not planning to be a regular developer on GitHub and contribute to the development, but if need be I could provide the code to whoever is interested.
There is some additional member functions to add and some more testing but I could perform the following so far:
import sock
s=sock.sock()
ip = sock.gethostbyname("google.com")
s.connect(ip, 80)
s.send(b"GET / HTTP/1.0\n\n")
print(s.recv(4096))
s.close()
the ouput...
>>> print(s.recv(4096))
recv: size alloc 4096
recv: size received 517
HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.ca/?gfe_rd=cr&ei=mi91 ... 8gfT6ICIAg
Content-Length: 258
Date: Thu, 15 May 2014 21:20:26 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.ca/?gfe_rd=cr&ei=mi91 ... g">here</A>.
</BODY></HTML>
I have developed a module: modsock
The implementation is based on the unix modsocket module but now it is available on the stmhal port using the Adafruit CC3000 module driver code.
I am new and getting up to speed on the MP C side of the code. So my implementation might have some issues due to the fact that not all the APIs/DEFINES are clear to me when using the Python/C interface.
NOTE: there were some issues using send and recv, these required some modifications to the buffer size found in cc3000_common.h
I am not planning to be a regular developer on GitHub and contribute to the development, but if need be I could provide the code to whoever is interested.
There is some additional member functions to add and some more testing but I could perform the following so far:
import sock
s=sock.sock()
ip = sock.gethostbyname("google.com")
s.connect(ip, 80)
s.send(b"GET / HTTP/1.0\n\n")
print(s.recv(4096))
s.close()
the ouput...
>>> print(s.recv(4096))
recv: size alloc 4096
recv: size received 517
HTTP/1.0 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.ca/?gfe_rd=cr&ei=mi91 ... 8gfT6ICIAg
Content-Length: 258
Date: Thu, 15 May 2014 21:20:26 GMT
Server: GFE/2.0
Alternate-Protocol: 80:quic
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.ca/?gfe_rd=cr&ei=mi91 ... g">here</A>.
</BODY></HTML>
Re: Socket module
just make a github account, fork the project, push your changes and submit a pull request. That is just the most convenient way to contribute for everyone..nelfata wrote:I am not planning to be a regular developer on GitHub and contribute to the development, but if need be I could provide the code to whoever is interested.
Re: Socket module
Hi,
We as a team are developing with the Micro Python board and we would be very happy if you could send / release the code to us, via github or any other means conveniant to you.
Thanks in advance,
Thomas
We as a team are developing with the Micro Python board and we would be very happy if you could send / release the code to us, via github or any other means conveniant to you.
Thanks in advance,
Thomas
- polygontwist
- Posts: 36
- Joined: Sat Jun 28, 2014 4:54 pm
- Location: Germany, Rostock
- Contact:
Re: Socket module
yes, please!
Re: Socket module
It looks like the wifi and ethernet code (along with the corresponding socket code) was just merged into master:
https://github.com/micropython/micropyt ... 90758217d7
and see the last comment on this thread:
https://github.com/micropython/micropython/issues/885
https://github.com/micropython/micropyt ... 90758217d7
and see the last comment on this thread:
https://github.com/micropython/micropython/issues/885
- polygontwist
- Posts: 36
- Joined: Sat Jun 28, 2014 4:54 pm
- Location: Germany, Rostock
- Contact:
Re: Socket module
thanks
- polygontwist
- Posts: 36
- Joined: Sat Jun 28, 2014 4:54 pm
- Location: Germany, Rostock
- Contact:
Re: Socket module
Hello,
i hav tested the socket and WLAN as client
(Micro Python v1.3.3-50-g9b6617e on 2014-10-11; PYBv1.0 with STM32F405RG
"pybv10-network-2014-10-11-v1.3.3-50-g9b6617e.dfu")
(http://www.binarytides.com/receive-full ... in-python/)
code snippet:
the problem is:
when I read an HTML page that is larger than the buffer, it is very fast;
is the buffer larger than the HTML page it takes a long time.
Unfortunately I could not get the bytes read individually.
A repeated s.recv(100) did not work:
I can read the data length before I define the bufferlength?
How can I read the individual bytes? (with s.setblocking(0) I could not read anything)
An my Server wrote many Error (not correkt GET...). but that's another story.
i hav tested the socket and WLAN as client
(Micro Python v1.3.3-50-g9b6617e on 2014-10-11; PYBv1.0 with STM32F405RG
"pybv10-network-2014-10-11-v1.3.3-50-g9b6617e.dfu")
(http://www.binarytides.com/receive-full ... in-python/)
code snippet:
Code: Select all
import socket
addr = socket.getaddrinfo("a-d-k.de", 80)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("IP:",addr[0][-1])
s.connect(addr[0][-1])
s.send("GET / HTTP/1.0\r\n\r\n")
s.setblocking(1)
#...
buf = s.recv(300)
print(buf)
#...
s.close()
when I read an HTML page that is larger than the buffer, it is very fast;
is the buffer larger than the HTML page it takes a long time.
Unfortunately I could not get the bytes read individually.
A repeated s.recv(100) did not work:
Code: Select all
while 1:
buf = socket.recv(50)
print("length:",len(data))
print(data)
if(len(data)==0)
break
How can I read the individual bytes? (with s.setblocking(0) I could not read anything)
An my Server wrote many Error (not correkt GET...). but that's another story.
Re: Socket module
I'd recommend that you bring this up as an issue on github (so Damien will see it - I think he reads github more than the forum).
- polygontwist
- Posts: 36
- Joined: Sat Jun 28, 2014 4:54 pm
- Location: Germany, Rostock
- Contact:
Re: Socket module
ok.
a pity the socket/Net/CC3000 is not written in uPhython, hacking it yourself...
a pity the socket/Net/CC3000 is not written in uPhython, hacking it yourself...
Re: Socket module
The cc3k.recv() method was trying to do multiple recv calls to get the requested buffer size. This was wrong behaviour that is now fixed.
I don't know if this is enough to fully fix your problem, but give this version a try: https://micropython.org/static/download ... e42570.dfu
I don't know if this is enough to fully fix your problem, but give this version a try: https://micropython.org/static/download ... e42570.dfu