Socket module

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Socket module

Post by nelfata » Thu May 15, 2014 9:30 pm

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>

stijn
Posts: 470
Joined: Thu Apr 24, 2014 9:13 am

Re: Socket module

Post by stijn » Sun May 18, 2014 10:56 am

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.
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..

thomasv
Posts: 2
Joined: Fri Sep 12, 2014 1:30 pm

Re: Socket module

Post by thomasv » Tue Sep 30, 2014 8:39 am

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

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: Socket module

Post by polygontwist » Wed Oct 01, 2014 7:57 pm

yes, please!

User avatar
dhylands
Posts: 3415
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Socket module

Post by dhylands » Wed Oct 01, 2014 8:51 pm

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

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: Socket module

Post by polygontwist » Sat Oct 11, 2014 6:40 pm

thanks :D

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: Socket module

Post by polygontwist » Mon Oct 20, 2014 5:28 pm

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:

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()
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:

Code: Select all

while 1:
  buf  =  socket.recv(50)
  print("length:",len(data))
  print(data)
  if(len(data)==0)
    break
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.

User avatar
dhylands
Posts: 3415
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Socket module

Post by dhylands » Mon Oct 20, 2014 6:26 pm

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).

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: Socket module

Post by polygontwist » Mon Oct 20, 2014 8:32 pm

ok.
a pity the socket/Net/CC3000 is not written in uPhython, hacking it yourself...

Damien
Site Admin
Posts: 634
Joined: Mon Dec 09, 2013 5:02 pm

Re: Socket module

Post by Damien » Wed Oct 22, 2014 12:17 am

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

Post Reply