python-escpos does not work

Hi,
I have configured my thermal printer locally and everything worked fine, when I implemented this new feature online, I get this error :

Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]: Internal Server Error: /myapp/commandefinish
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]: Traceback (most recent call last):
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/asgiref/sync.py", line 330, in thread_handler
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     raise exc_info[1]
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 38, in inner
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     response = await get_response(request)
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 231, in _get_response_async
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     response = await wrapped_callback(request, *callback_args, **callback_kwargs)
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/asgiref/sync.py", line 296, in __call__
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     ret = await asyncio.wait_for(future, timeout=None)
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/usr/lib/python3.10/asyncio/tasks.py", line 408, in wait_for
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     return await fut
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/usr/lib/python3.10/concurrent/futures/thread.py", line 58, in run
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     result = self.fn(*self.args, **self.kwargs)
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/asgiref/sync.py", line 334, in thread_handler
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     return func(*args, **kwargs)
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     return view_func(request, *args, **kwargs)
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/myapp/views.py", line 704, in commandefinish
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     printer = Network('***.***.*.**', 9100)
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/escpos/printer.py", line 193, in __init__
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     self.open()
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:   File "/home/wahab/apporder/env/lib/python3.10/site-packages/escpos/printer.py", line 199, in open
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]:     self.device.connect((self.host, self.port))
Mar  9 18:55:53 django411onubuntu2204-s-1vcpu-1gb-amd-ams3-01 gunicorn[1771129]: TimeoutError: timed out

my code :

    printer = Network('***.***.*.**', 9100)
    printer.text('www.all-in-one1.fr')
    printer.cut()

I don’t understand why it works locally and doesn’t work online, but I have configured everything the same

Are you saying that you’ve got a server “somewhere else” that is trying to connect to a device within your local network?

If so, then you need to ensure that there’s no firewall, NAT, or anything else that would inhibit or prevent the traffic.

Also, is your printer address a global IP address or a local address?

I connect them with a network cable, so I guess it’s a local address

Ok, that doesn’t necessarily mean anything.

Is the IP address assigned to that device a non-routable address?

yes the ip is not routable non-routable address

That means that your site, if it’s external to your network, cannot address it directly. You’d need to configure your router / NAT / whatever to forward port 9100 to your printer, and change the configuration of your server to connect to your public IP address.

So, if I understand correctly, I have to configure my router to accept the IP address and all other information from my server?

No. You need to configure your router to forward any request coming in on port 9100 to your printer.

You then need to configure your server to issue its requests to your public IP address.

I have configured my router to forward all incoming IP to my printer, but when you say configure my server to send requests to my public IP address, do you mean the printer’s address?

No, I’m referring to what your router would generally call it’s WAN address. It’s the public IP address where traffic from outside your network gets routed to.

I am also facing same problem…i have connected my printer to router and started printing bills via localhost…there is no issue there…but when i deployed the project using godaddy cpanel…my bills not get printed and i am getting timeout error caused due to network failure

If you’re looking for assistance with this, I’d suggest you open up a new topic to discuss it. (Also, have you read through this entire thread? It starts to address some of the issues that may be involved.)