I m trying to read data from a GPS device "Telonika FMB204" using a python socket and TCP/IP
all you have to do as setup is to specify the server IP and the port the start my python I found Telonika forum showing data received successfully, I try to test it using my dynamic IP address but I the script shows no data my question is there any way to host the script somewhere ?
link :https://community.teltonika-gps.com/4965/how-to-read-data-from-teltonika-fmb001-with-a-python-script
import socket
port = 3000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', port))
s.listen(1)
conn, addr = s.accept()
print('Connected by ', addr)
imei = conn.recv(1024)
conn.send('\x01')
while True:
try:
data = conn.recv(1024)
if not data:
break
f = open("demofile2.txt", "a")
f.write(data)
f.close()
print(data)
except socket.error:
print("Error Occured.")
break
Receiving data from GPS Device will work only on computer with a fixed IP address (you can use no-ip service and open port on your router in my case the port 3000 ) or you can buy a VPS server open the port and you are ready to go
hope those informations will help ;)
Related
I have created a simple python server that sends back everything it receives.
I wonder if it would be possible to close the connection immediately after sending the data to the client (web browser), and the client then displays the data it has received. Currently, the client displays The connection was reset.
Thanks
#!/user/bin/env python3
import socket
HOST = 'localhost' # loopback interface address
PORT = 3000 # non-privileged ports are > 1023
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
while True:
# connect to the next client in queue
conn, addr = s.accept()
with conn:
print('Connected by', addr)
data = conn.recv(1024).decode()
print(data)
conn.sendall(data.encode())
Solution
#!/user/bin/env python3
import socket
HOST = 'localhost' # loopback interface address
PORT = 3000 # non-privileged ports are > 1023
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
while True:
# connect to the next client in queue
conn, addr = s.accept()
print('Connected by', addr)
data = conn.recv(1024).decode()
print(data)
conn.sendall(data.encode())
conn.shutdown(socket.SHUT_WR) # changed
Explanation
The solution uses conn.shutdown(socket.SHUT_WR) which sends a FIN signal instead of conn.close() which sends a RST signal.
We don't want to force the connection to close. TCP setup is expensive, so the browser would ask the server to keep the connection alive to query assets, and in this example the browser asks for style.css.
RST confuses the browser, and Firefox will show you "connection reset" error.
FIN says: "I finished talking to you, but I'll still listen to everything you have to say until you say that you're done."
RST says: "There is no conversation. I won't say anything and I won't listen to anything you say."
from FIN vs RST in TCP connections
Here is the Wireshark capture of using shutdown, we see that both the server at 3000 and the browser acknowledged each other's FIN.
When using close, we see that
A better option would be to wait for the client to initiate the 4 way finalization by programming the server to shut down the socket when the client signals FIN.
The 4 way finalization
From https://www.geeksforgeeks.org/tcp-connection-termination/
I am trying to build a connection between a server and one or more clients in Python using sockets. My code works just fine when I connect with a client in the same network, but my goal is to build a program which me and my friend can use, so I want to figure out a way to connect to my server from an external network via the internet.
My server-side code looks like this:
import socket
server = "internal_ip"
port = 5006
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
serv.bind((server, port))
print(server)
except socket.error as e:
print(e)
serv.listen(2)
while True:
conn, addr = serv.accept()
from_client = ""
while True:
data = conn.recv(4096)
if not data:
break
from_client += str(data)
print(from_client)
conn.send(str.encode("I am SERVER"))
conn.close()
print("Client disconnected")
And this is my client:
import socket
server = "internal_ip"
port = 5006
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
client.connect((server, port))
except socket.error as e:
print(e)
while True:
try:
client.send(str.encode("I am CLIENT"))
from_server = client.recv(4096)
#client.close()
print(from_server)
except:
break
client.close()
This works just fine within a network. Then I started testing the code from an external client. I changed the ip to my external ip address which I have found using whatismyipaddress.com, and port number to a different one than I use on the server side.
server = "external_ip"
port = 5007
Then I enabled port forwarding using cmd:
netsh interface portproxy add v4tov4 listenport=5007 listenaddress=external_ip connectport=5006 connectaddress=internal_ip
I get WinError 10060. Tried switching firewall on and off, and allowing these specific ports, but I can't make it work.
Can you help me with my problem please?
I'm trying to make a chat app in Python and I'm having some trouble.
I made a server on which I can connect successfully by using the local IP address. However, when I try to connect to it on an another device with my public IP address, there seems to be a timeout, no errors occur and it's continuously trying to connect.
Edit: I've already set up port-forwarding for my IPv4 address. And the client is using the public IP.
server.py:
import socket
s = socket.socket()
host = socket.gethostbyname(socket.gethostname())
port = 2000
s.bind((host, port))
print("Server started, waiting for incoming connections")
s.listen(5)
connection, address = s.accept()
print("New connection from", address)
while True:
data = connection.recv(1024).decode()
print("received:", data)
ret = data + "+++++++"
connection.send(ret.encode())
client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = #my public ip address from whatsmyip.com
port = 2000
s.connect((host, port))
print("Connected.")
while True:
message = input("msg: ")
s.send(message.encode())
data = s.recv(1024).decode()
print(data)
Well, first of all, is your server in a network with other devices? If you have a router there, the IP you see in whatsmyip.com is the router's, not your computer's, IP. So you'd be trying to connect to it.
You can check that with the command netstat.
I'm new to python and this time I want to send a file between two VMs, first of all, the VMs are configured with NAT Network, both VMs can ping each other. The codes are the following:
Server side:
#server.py
import socket # Import socket module
port = 60000 # Reserve a port for your service.
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
# s.bind((host, port)) # Bind to the port
s.bind(('', port))
s.listen(5) # Now wait for client connection.
print 'Server listening....'
while True:
conn, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
data = conn.recv(1024)
print('Server received', repr(data))
filename='runbonesi.py'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
print('Done sending')
conn.shutdown(socket.SHUT_WR)
print conn.recv(1024)
conn.close()
client side:
#client.py
import socket # Import socket module
s = socket.socket() # Create a socket object
#host = socket.gethostname() # Get local machine name
host = '10.0.2.15'
port = 60000 # Reserve a port for your service.
s.connect((host, port))
with open('received_file', 'wb') as f:
print 'file opened'
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s', (data))
if not data:
break
# write data to a file
f.write(data)
f.close()
print('Successfully get the file')
s.close()
print('connection closed')
The results can be seen in the following pictures:
Server side:
result on server
Client side:
result on client
The problem is, the client didn't receive the file as .py format, it only received as txt files. Please help me resolve this issue.
Thank you.
After s.connect((host, port)) in the client script you have to write s.send(...) with 'hello', for example. You also forgot to write the file extension '.py' in with open(...) (only if you want to save the file as Python script!). And you also have to add s.send(...) before you close the connection on the client side. 😉
I'm trying to take incoming data from one port and stream it to a port on another computer. Here is what I have so far:
import socket
port = 8787
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
print ("waiting on port:", port)
while 1:
data, addr = s.recvfrom(1024)
print (data)
`
So I can stream the data in from the port using the script above no problem. What I need to do is now take that data stream and forward it to a port on another computer. I came across this:
#!/usr/bin/python
from socket import *
bufsize = 1024 # Modify to suit your needs
targetHost = "192.1.1.2"
listenPort = 8788
def forward(data, port):
print "Forwarding: '%s' from port %s" % (data, port)
sock = socket(AF_INET, SOCK_DGRAM)
sock.bind(("localhost", port)) # Bind to the port data came in on
sock.sendto(data, (targetHost, listenPort))
def listen(host, port):
listenSocket = socket(AF_INET, SOCK_DGRAM)
listenSocket.bind((host, port))
while True:
data, addr = listenSocket.recvfrom(bufsize)
forward(data, addr[1]) # data and port
listen("localhost", listenPort)
But I'm not sure where to put the outgoing port. The IP for the home server that I'm pulling data on has an IP of say 192.1.1.1 and is streaming data in from port 8787. The remote server has an IP of say 192.1.1.2 and it's listening on port 8788. I'm not sure where I need to put port 8787, which is where the home server is pulling in the data from. Any suggestions would be most helpful. Thanks!
If that last code you quoted runs on your 192.1.1.1, you need to call listen("localhost", 8787) in the last line instead of listen("localhost", listenPort), and you should be fine. listenPort refers to the port where the server that the data is being forwarded to (192.1.1.2) is listening, while listen expects the port where the current machine (192.1.1.1) listens for input. listen then calls forward (telling it to use the same port for the outgoing connection).