Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to be able to read and write with a USB port from a remote machine as if it were local. I want to do this by writing a python script that establishes a TCP connection to the remote machine and then constantly reads from the USB port and write to the TCP connection and vice versa. What is the best way to code this up in Python simply and quickly?
I had to do the same thing you're asking for a robotics project I had in the past year. We had a Raspberry Pi constantly reading on a USB port linked to an Arduino board, and as soon as it got some message it sent it throught TCP to all the remote clients connected.
The project is called autonomee and is available on github.
To summarize, you have to do the following:
The 'client' connects to the server that is linked to the USB "source"
Have a thread (on the server) constantly reading from the USB (I'd recommend using pyserial or pyusb for that)
When you receive some data, send it throught TCP to the remote client (more on that below)
The remote client keeps listening for data and whenever it gets a message it processes it
The most thought part is the TCP connection, and it's not that hard.
You can either use twisted for a higher level TCP server or just use the standard TCPServer class (we did the latter). Check the examples on the SocketServer doc, they are really useful !
I can't give you much more detail as it highly depends on which kind of data you have to send, at which frequency, but I'd advise you to have a look at the code I've produced for the server and the client
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
I write a custom vpn project in python using TUN/TAP interface and UDP Socket connection for my self, and its work fine and every data that comes from the tun will encrypt and will goes through the udp connection securly and in server side comes from UDP and its decrypt and then sends to TUN interface.
in both side ethernet NAT with the TUN and in client its connect to my local.
All Pings transfer completely and all NAT works fine in server and client.
but when i want to open a google website or other websites, browser says ssl/tls insecure connection.
how it can findout this? and whats the way to solve this?
I test the websites from deferent computer in my local network and its not my browser or clock problem.
I know it is the problem of my project or NAT rules, but i dont know where it is?
I test my connection and NAT rulse and all of them works fine, and ping the google website through the VPN transfer completly.
I can ping everywhere but i cannot open any website because of ssl/tls insecure connection.
Please, I need some help!
I'm currently working on client-server program (sockets, Python).
I decided to build it like in this guide (https://codesource.io/creating-python-socket-server-with-multiple-clients/).
Each client has its own thread and connections are accepting in an infinitive loop. Sockets are using TCP.
According to the task server should support connection with at least 50 clients simultaneously.
How can I ensure it?
I can't find anything related to this problem.
Thanks in advance!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have a Django project on my laptop. It works perfectly fine on my machine.
This web app takes input from the user in the form of an image and reads the content in it. If the content is already present on the database, it runs a Linux command on a different machine. Both machines are connected to the same network.
Currently, I am using SSH to connect to the other machine but it asks for password every time. Is there any way I can eliminate the need for entering a password every time I want to run a command?
Solution 1: use SSH pre-shared key to login via SSH without a password. See this link for how to do it. After you have configured it properly, you are able to run a command on your server:
ssh hostname-or-ip-of-the-raspi command arg1 arg2 ...
and will execute command arg1 arg2 ... on the Raspberry PI, without being prompted for a password.
Solution 2: use TCP communication, and write a server for the Raspberry PI and a client for your server. You can use raw sockets, or some high level library such as zmq.
I am considering you are a simple intermediate programmer and based on that giving you two solutions with their pros and cons.
Solution 1: Using a simple Flask app on Raspberry PI
You can modify the following code to request a simple app running on PI to perform any actions.
Code:
from flask import Flask
app = Flask(__name__)
#app.route('/runMotor')
def hello_world():
runMotor()
# Run any script here
return "Motor Ran"
You can then use your raspberry to call something like:
<your_rasp_ip>:<port>/runMotor
Pros: Easy to implement, You can even move it further to be used from outside your firewall.
Cons: Slow and would not be suitable for very rapid concurrent request. Concurrency is the downfall (or you can queue the requests and then keep a check for this issue)
Solution 2 Using MQTT: MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport.
You can play with your code, checkout example here
Pros: Extremely lightweight and removes concurrency overhead, MQTT is an asynchronous messaging protocol. This is best used for real time systems.
Cons: MQTT is a very light messaging protocol and cannot support heavy payloads.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Purpose:
I'm making a program that will set up a dedicated server (software made by game devs) for a game with minimal effort. One common step in making the server functional is port forwarding by making a port forward rule on a router.
Me and my friends have been port forwarding through conventional means for many years with mixed results. As such I am hoping to build a function that will forward a port on a router when given the internal ip of the router, the internal ip of the current computer,the port and the protocol. I have looked for solutions for similar problems, but I found the solutions difficult to understand since i'm not really familiar with the socket module. I would prefer not to use any programs that are not generally installed on windows since I plan to have this function work on systems other than my own.
Approaches I have explored:
Creating a bat file that issues commands by means of netsh, then running the bat.
Making additions to the settings in a router found under Network -> Network Infrastructure (I do not know how to access these settings programmaticly).
(I'm aware programs such as GameRanger do this)
Using the Socket Module.
If anyone can shed some light how I can accomplish any of the above approaches or give me some insight on how I can approach this problem another way I would greatly appreciate it.
Thank you.
Edit: Purpose
You should read first some sort of informations about UPnP (Router Port-Forwarding) and that it's normally disabled.
Dependent of your needs, you could also try a look at ssh reverse tunnels and at ssh at all, as it can solve many problems.
But you will see that working with windows and things like adavanced network things is a bad idea.
At least you should use cygwin.
And when you really interessted in network traffic at all, wireshark should be installed.
I'm not sure if that's possible, as much as I know, ports aren't actually a thing their just some abstraction convention made by protocols today and supported by your operating system that allows you to have multiple connections per one machine,
now sockets are basically some object provided to you by the operating system that implements some protocol stack and allows you to communicate with other systems, the API provides you some very nice API called the socket API which allows you use it's functionality in order to communicate with other computers, Port forwarding is not an actual thing, it just means that when the operating system of the router when receiving incoming packets that are destined to some port it will drop them if the port is not open, think of your router as some bouncer or doorman, standing in the entrance of a building, the building is your LAN, your apartment is your machine and rooms within your apartment are ports, some package or mail arrives to your doorman under the port X, a port rule means on IP Y and Port X of the router -> forward to IP Z and port A of some computer within the LAN ( provides and implements the NAT/PAT ) so what happens if we'll go back to my analogy is something such as this: doorman receives mail destined to some port, and checks if that port is open, if not it drops the mail if it is it allows it to go to some room within some apartment.. (sounds complex I know apologize) my point is, every router chooses to implement port rules or port blocking a little bit different and there is no standard protocol for doing, socket is some object that allows you program to communicate with others, you could create some server - client with sockets but that means that you'll need to create or program your router, and I'm not sure if that's possible,
what you COULD do is:
every router provides some http client ( web client ) that is used to create and forward ports, maybe if you read about your router you could get access to that client and write some python http script that forwards ports automatically
another point I've forgot is that you need to make sure you're own firewall isn't blocking ports, but there's no need for sockets / python to do so, just manually config it
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a multi-client server code in which multiple clients are connected to a server.
Now all the clients are sending text messages to the server and are receiving a reply from the server. Now if one client tries to flood the server using a syn flood which the client runs in a different terminal then I want the server to close the connection of the client and the server still continues messaging with the other clients.
In short its how to prevent a DOS attack?
When dealing with a syn flood, you don't do that in Python. A syn flood is one machine sending a large amount of syn packages to your server, leading to an exhaustion of kernel resources.
The attack takes advantage of the fact that connection attempts are fully handled by the kernel. As such, your application will only get notified of a new connection after it was fully created (which will not happen in a syn-flood). Instead, your socket's backlog will quickly fill up and there will be no more connections attempts possible until the half-open connections time out.
As such, you'll have to handle this in the kernel, e.g. by increasing the socket's backlog (note that even half-open connections require some memory unless you use syn-cookies or similar) or by limiting the amount of syn-packets which are allowed to be received e.g. with iptables or other firewalls.
Generally, if the socket's backlog is full, no new connections will be accepted. Existing connections will not be affected by this and will continue to be served. Generally, when receiving a syn-flood, there are other kernel resources seriously strained which means, you might still have problems communicating depending on the actual circumstances.
To say it clear again: handling syn-floods is not a thing you'll be able to handle in Python but have to deal with by properly configuring your kernel.