I've gotten a chance to work again with Python, but this time I decided to take Python 3.5 to my journey.
I had to port a working non-blocking socket server using Tornado, from Python 2.7 to 3.5. Used same source code, but this time it doesn't work as needed.
I keep getting [WinError 10035] A non-blocking socket operation could not be completed immediately on send whenever I accept a socket connection using socket.accept() and I still can't figure out why.
Tried to use example code that I've found a few years ago on GitHub Gist and still keep getting an error. Is there any changes in socket library or is it just a bug?
This error is harmless and expected. The problem is that the gist you linked to doesn't know about windows-specific error codes (on line 24 it checks for EWOULDBLOCK and EAGAIN, but it should also use WSAEWOULDBLOCK).
Since that gist was written, Tornado has gained some new utilities to make this easier. If you're using IOStreams, you can use tornado.tcpserver.TCPServer to accept your connections, or if you want to continue using plain sockets you can use the lower-level tornado.netutil.add_accept_handler.
Related
Maybe is my question a little bit weird, but I am looking for an answer since couple days.
The only thing what I found were sockets and asyncio(dont really know what it is) , but with sockets you can establish a connection just in your localhost.
Is there any otherways to creat a connection between two quite different pcs or between a client and server wireless.
If you have any ideas just write, maybe it could be helpful. As a beginner in python, I would be happy if you guys could show me some way to find easy tutorials (maybe some websides) and important things to learn. So everything could be helpful, I am just curious. :)
You can create a server / client pair, through executables running on both PCs. TCP and UDP sockets can be used to communicate outside 'localhost'. You can do this rather easily in many programming languages like C, C#, python amongst others.
what you can do is create two seperate python files: client.py and server.py
The client.py will be trying making the connection. the server.py will be trying to receive that connection. You mentioned you weren't familiar with socket. Socket is a low-level networking interface when you import the library socket to a file, you are able to make system calls between different computers to interact with each other.
You need to install ros library form here for example if you have Ubuntu 16.04.
after that it is very easy to use those lines
We used this method with robots.
Recently I have been tasked with a project involving reading Modbus data, specifically with the pymodbus package. before diving into this project I wanted to simulate some reading and writing of Modbus data (without having to use a machine) with python. Using the sample data they have on their homepage, I try to write using the client but encounter a Modbus error
Ive tried looking into the matter to see what I could dig up wonder if I need a server to write or read. However I am unsure as with my experiences with socket and serial I just needed an established connection with the correct port to simply write (however I understand that Modbus is different).
Here is the code
client = ModbusTcpClient('localhost')
client.write_coil(1, True)
result = client.read_coils(1,1)
print(result.bits[0])
client.close()
and here is my error "pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(localhost:502)]"
I expect the output to simply write Modbus without needing anything no necessarily listen on the other end, but instead I keep getting an error when trying to connect/write. (sorry if this is hard to comprehend, my brain is all over the place and I am extremely new to Modbus in general).
If you want to send Modbus queries and you don't have any Modbus hardware yet you need to run a dummy Modbus server on your computer.
You can take a look at the examples.
You might also need to add a rule to your firewall for port 502. If you are on Linux you can just switch ports to a higher number like 5020 on both ends to avoid this problem.
I had a tcp proxy in python the version is 2.6.
It works fine in any cases with following logic
client ---> proxy ---> server
I wrapped the tcp with tls from proxy to server.
client ---> proxy ==++ssl++==> server
That works fine in some cases and fails in others.
The error is that the server is waiting for more information from the client, but client sends nothing more. At the 26th round trip.(Certainly, the round trip number of successful case also larger than 26.)
I cannot tell more about the detail but I thought the SSL should be transparent to the logic.
Any Idea that part of the functionality fails? How should I debug it?
Edit: In python 2.6, the tls version can only be 1.0.
It is hard to tell what you are doing without any example demonstrating the problem but depending on how your application works SSL/TLS is not just a transparent replacement for TCP sockets. While it might be transparent in most cases if you use only blocking sockets it gets different with non-blocking I/O. In this case you have to deal with user space buffering where select will not report available data even thought there are unread data. You also have to deal with situations where you temporarily fail to write because the TLS stack needs a read first or the other way.
For more details about differences with non-blocking I/O and select see Behavior of python's select() with partial recv() on SSL socket or select and ssl in python. Additionally non-blocking I/O needs special handling with accept and connect too but I doubt that there is useful support for it in the old python version you are using.
I was looking at the socket programming module of the python standard library and I noticed a fucntion socket.setblocking. The documentation mentioned that setting a socket to non blocking mode would mean that an error would be raised if the data was not sent out through the socket immediately or if data was not available upon trying to read from the socket.
I'm having trouble understanding usecases in which this function might be useful. I'm working on a Linux machine(just in case the answer to this would be OS dependent).
Thanks!
When you set the socket to blocking, the socket waits for the specified time on that socket. While it is waiting on the socket, your program cannot do anything. At the end of the wait time it raises an error. Sometimes you dont want blocking to occur.
A good use case for this might be when you are sending an receiving message on a single threaded program using multiple sockets. You don't want to block on a socket while waiting to send or receive messages rather you may want to check if there are messages to send or receive for each of the sockets hence you would want no blocking time or limited blocking time while you loop through the sockets. This will provide a more in depth discussion of python sockets.
Can someone please tell how to write a Non-Blocking server code using the socket library alone.Thanks
Frankly, just don't (unless it's for an exercise). The Twisted Framework will do everything network-related for you, so you have to write only your protocol without caring about the transport layer. Writing socket code is not easy, so why not use code somebody else wrote and tested.
Why socket alone? It's so much simpler to use another standard library module, asyncore -- and if you can't, at the very least select!
If you're constrained by your homework's condition to only use socket, then I hope you can at least add threading (or multiprocessing), otherwise you're seriously out of luck -- you can make sockets with timeout, but juggling timing-out sockets without the needed help from any of the other obvious standard library modules (to support either async or threaded serving) is a serious mess indeed-y...;-).
Not sure what you mean by "socket library alone" - you surely will need other modules from the standard Python library.
The lowest level of non-blocking code is the select module. This allows you to have many simultaneous client connections, and reports which of them have input pending to process. So you select both the server (accept) socket, plus any client connections that you have already accepted. A thin layer on top of that is the asyncore module.
Use eventlets or gevent. It monkey patches existing libraries. socket module can be used without any changes. Though code appears synchronous, it executes asynchronously.
Example:
http://eventlet.net/doc/examples.html#socket-connect