The following
let new_socket () = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
let socket_address = Network.make_address "127.0.0.1" 7777 in
let listening_socket = new_socket () in
Lwt_unix.setsockopt listening_socket Unix.SO_REUSEADDR true;
Lwt_unix.bind listening_socket socket_address;
results into this error:
Error: correctness:2:remote_client:0:set
Unix.Unix_error(Unix.EADDRINUSE, "bind", "")
Raised at file "src/core/lwt.ml", line 782, characters 22-23
Called from file "src/unix/lwt_main.ml", line 34, characters 8-18
Called from file "src/oUnit.ml", line 597, characters 6-10`
Last line fails to grant the bind, ideas why, how to pass this?
The error is clearly stated: the address you are trying to bind to is already in use, a socket is is already bound on port 7777.
To understand the codes of the Unix_error raised by a function look in the manual of the corresponding C unix function, in that case bind(2).
Related
I am trying to invoke my Lambda function using sam local invoke but find that it cannot connect to my host MySQL. I tried adding --docker-network host but it also cannot connect
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/docker/api/client.py", line 229, in _raise_for_status
response.raise_for_status()
File "/usr/lib/python3.6/site-packages/requests/models.py", line 935, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http+docker://localhost/v1.35/networks/6ad3bd87e8437e8410145d169a4edf68d1b0247a67257ce7dd1208dac3664c82/connect
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/bin/sam", line 11, in <module>
load_entry_point('aws-sam-cli==0.5.0', 'console_scripts', 'sam')()
File "/usr/lib/python3.6/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/usr/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/lib/python3.6/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File "/usr/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/samcli/commands/local/invoke/cli.py", line 47, in cli
docker_network, log_file, skip_pull_image, profile) # pragma: no cover
File "/usr/lib/python3.6/site-packages/samcli/commands/local/invoke/cli.py", line 79, in do_cli
stderr=context.stderr)
File "/usr/lib/python3.6/site-packages/samcli/commands/local/lib/local_lambda.py", line 80, in invoke
stdout=stdout, stderr=stderr)
File "/usr/lib/python3.6/site-packages/samcli/local/lambdafn/runtime.py", line 83, in invoke
self._container_manager.run(container)
File "/usr/lib/python3.6/site-packages/samcli/local/docker/manager.py", line 61, in run
container.create()
File "/usr/lib/python3.6/site-packages/samcli/local/docker/container.py", line 115, in create
network.connect(self.id)
File "/usr/lib/python3.6/site-packages/docker/models/networks.py", line 57, in connect
container, self.id, *args, **kwargs
File "/usr/lib/python3.6/site-packages/docker/utils/decorators.py", line 19, in wrapped
return f(self, resource_id, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/docker/api/network.py", line 248, in connect_container_to_network
self._raise_for_status(res)
File "/usr/lib/python3.6/site-packages/docker/api/client.py", line 231, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/usr/lib/python3.6/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 400 Client Error: Bad Request ("container cannot be disconnected from host network or connected to host network")
I noticed the last line:
docker.errors.APIError: 400 Client Error: Bad Request ("container cannot be disconnected from host network or connected to host network")
How do I fix this?
I can't say why --docker-network host produces this error, but I don't think you need it. I can connect to MySQL running on my local machine from within SAM local, without any network overrides.
I do this by simply using my local IP (as opposed to localhost or 127.0.0.1) to connect to the database.
The following lambda function connects to my local MySQL just fine, provided I use my local IP, as revealed a tool like ipconfig.
'use strict';
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.1.6',
user : 'mike',
password : 'password',
database : 'logtest'
});
module.exports.hello = (event, context, callback) => {
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
const response = {
statusCode: 200,
body: JSON.stringify({solution: results[0].solution})
};
connection.end();
callback(null, response);
});
};
Partial ipconfig output:
Ethernet adapter Local Area Connection 6:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::95c2:495c:7226:8ac2%39
IPv4 Address. . . . . . . . . . . : 10.251.19.6
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::496a:d34d:8380:3b1c%15
IPv4 Address. . . . . . . . . . . : 192.168.1.6
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1
In this case, my lambda function can connect to local MySQL using 10.251.19.6 or 192.168.1.6.
Minimal example here.
Could you post how you are starting mysql container locally? Hard to say whats going wrong with out more info.
Here is some general advice though.
If your running on a mac
You can just use special host name locally when configuring mysql client in container making the call. It will resolve locally with no --docker-network flag needed.
Ex:
host = 'docker.for.mac.localhost'
Or on newer installs of docker https://docs.docker.com/docker-for-mac/release-notes/#docker-community-edition-17120-ce-mac49-2018-01-19
host = 'docker.for.mac.host.internal'
Ex python code.
import mysql.connector
host = 'docker.for.mac.host.internal'
cnx = mysql.connector.connect(user='scott', password='password',
host=host,
database='employees')
cnx.close()
If you are not on mac
Then make sure you are passing --docker-network when you start your mysql container it should look something like this.
docker run -d -v "$PWD":/var/lib/mysql -p 3306:3306 \
--network lambda-local \
--name mysql \
mysql
Then when invoking locally
sam local invoke --docker-network lambda-local
Then in your lambda code you just use network --name you passed mysql container as hostname.
Ex:
import mysql.connector
host = 'mysql'
cnx = mysql.connector.connect(user='scott', password='password',
host=host,
database='employees')
cnx.close()
No configuration need just add below host while connection to your mysql
for windows : docker.for.win.localhost
for mac : docker.for.mac.localhost
const con = require('serverless-mysql')({
config: {
host : 'docker.for.win.localhost',
database : 'db-name',
user : 'root',
connectTimeout : 5000,
password : ''
}
});
You can check cases as follows:
Whether the docker port has a mapping.
Check the port of AWS's security group.
I have just started learning python. I just got to the chapter of sockets and came across the following code:
import socket
import sys
HOST = ''
PORT = 4444
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Our socket is created"
try:
s.bind((HOST, PORT))
except socket.error as e:
print "Error in binding the port"
sys.exit()
print "Binding is complete"
s.listen(20)
print "Server is in listen mode now!"
while 1:
conn, addr = s.accept()
print "Connected with the : " + addr [0] + ' : ' +str(addr[1])
s.close()
When i run the code it works fine, but when i try to connect to the port on 4444 i get the following error in the console which i can't figure out why:
I am trying to connect to localhost on port 4444 using putty, i tried ssh, telnet and raw and all of them yield the same error
Our socket is created
Binding is complete
Server is in listen mode now!
Connected with the : 127.0.0.1 : 49278
Traceback (most recent call last):
File "C:\pythontraining\Module12\server-working.py", line 24, in <module>
conn, addr = s.accept()
File "C:\Python27\lib\socket.py", line 206, in accept
sock, addr = self._sock.accept()
File "C:\Python27\lib\socket.py", line 174, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
I think the error occurred from the socket that has not been available even the connection thread is alive.
Try fix indention on
s.close()
.
.
while 1:
conn, addr = s.accept()
print "Connected with the : " + addr [0] + ' : ' +str(addr[1])
s.close()
I have implemented a script to parse ERF file to get the DNS records from the packets. The script works under Linux but DOES NOT work under Windows.
I have tried to simplify it and read only two packets from the file and the result was totally wrong.
Here is the output for the first two packets:
rlen 1232
wlen 1213
ts: (5822080496043415499L,)
rec len: 1232
protocol: 6 rem 1180
tcp
src port: 59626
remaining length based on the erf header 1160 remaining length based in IP total length 1155
----------------------
rlen 44076
wlen 13638
ts: (246640611164160L,)
rec len: 44076
protocol: 9 rem 44024
----------------------
for the first packet the output is correct, but for the second one everything is wrong. What I did was reading the record length from the ERF header to keep track of the packet boundaries. When I printed the payload of the tcp I found that the erf header of the next packet was in the payload of tcp. This problem didn't occur when I ran the code under linux.
Can anyone tell me what I'm doing wrong?
Here is my code:
if __name__ == '__main__':
argv= sys.argv
outputFile=''
inputFile=''
dnsPacketCounter=0
ethH = {}
ipHeader = {}
ipH = {}
totalPackets=0
if len(argv) ==1:
print 'erfParser.py -i <inputfile> -o <outputfile>'
sys.exit(0)
elif len(argv) == 2:
if argv[1] == '-h':
print 'erfParser.py -i <inputfile> -o <outputfile>'
sys.exit(0)
elif len(argv) == 5:
if argv[1] == '-i':
inputFile = argv[2].strip()
elif argv[3] == '-i':
inputFile = argv[4].strip()
if argv[1] == '-o':
outputFile = argv[2].strip()
elif argv[3] == '-o':
outputFile= argv[4].strip()
else:
# Open the trace file
print 'erfParser.py -i <inputfile> -o <outputfile>'
sys.exit(0)
try:
packets = open(inputFile , 'r+')
except IOError:
print 'The file: ',inputFile,' not found.'
sys.exit(0)
try:
outFile=open(outputFile+'.txt', 'w+')
except IOError:
print 'The file: ',outputFile,' can not be opened.'
sys.exit(0)
ts=packets.read(8)
i=0
while ts:
erf={}
hdr = packets.read(8)
#print ts.encode('hex')
totalPackets=totalPackets+1
erf= getERFHeader(ts,hdr)
print 'rlen',erf['rlen']
print 'wlen',erf['wlen']
print 'ts: ',erf['ts']
remainingLength=erf['rlen']- 16
print 'rec len: ',erf['rlen']
if erf['type'] == 0x07:
ext=packets.read(8)
remainingLength=remainingLength- 8
pad=packets.read(2) # pad
remainingLength=remainingLength- 2
ethH= packets.read(14) # ethernet header `16 bytes
remainingLength=remainingLength- 14
ipHeader= packets.read(20) #ip header length is 20 bytes
remainingLength=remainingLength- 20
ipH= getIPHeader(ipHeader)
print 'protocol: ',ipH['protocol'],' rem ',remainingLength
if ipH['protocol'] ==TCP:
print 'tcp'
hdr = packets.read(20)
remainingLength=remainingLength- 20
tcpHeader=getTCPHeader(hdr)
tcpPayload= packets.read(remainingLength)
print 'src port: ',tcpHeader['srcPort']
# print 'tcp payload in hex: ',tcpPayload.encode('hex')
print 'remaining length based on the erf header',remainingLength,'remaining length based in IP total length' ,ipH['totalL']-40
print '----------------------'
ts=packets.read(8)
i=i+1
if i==2:
break;
pass
Can anyone tell me what I'm doing wrong?
Yes, I can tell you that you're opening the file in text mode rather than binary mode:
packets = open(inputFile , 'r+')
To quote the Python documentation for open():
Modes 'r+', 'w+' and 'a+' open the file for updating (reading and writing); note that 'w+' truncates the file. Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.
UN*Xes such as Linux are "systems that don't have this distinction", because the Python open() is modeled after the UN*X version of the "standard I/O library", in which lines end with a \n. On Windows, lines end with \r\n, and opens in the "standard I/O library" can either:
open in text mode, in which the \r\n at the ends of lines, when read, are shown to the program as a \n, and a \n, when written to a line, is written as \r\n, so that programs written for UN*X can work on Windows without having to worry about the end-of-line sequence;
open in binary mode, in which case a read gives you exactly the bytes that are in the file, and a write puts the bytes given to it into the file;
so it's a system that "[differentiates] between binary and text files", at least in some of the I/O libraries. (At the lowest level of I/O, namely the CreateFile(), ReadFile(), and WriteFile() calls, Windows makes no such distinction - it treats files as sequences of raw bytes, with no "open as text" option, just as UN*X systems do with open(), read(), and write() - but at all levels of I/O intended to be somewhat UN*X-compatible, they provide a text vs. binary option.)
ERF files are binary files, so you need to open with 'rb+' or 'r+b', not 'r+'. That will make no difference on UN*Xes such as Linux, but will give you raw binary data on Windows.
(Actually, just 'rb' will suffice - if you don't plan to write to the file you're reading, the + isn't necessary, and creates the risk of accidentally overwriting the file.)
I am implementing a code through which i have to get devices connected to all network interfaces on my machine.
For this, i am first getting the ip of all network interfaces and then sending m-search command on them.
After 2.5 seconds port is stopped to listen.
But it is giving me some assertion error.
Code:
class Base(DatagramProtocol):
""" Class to send M-SEARCH message to devices in network and receive datagram
packets from them
"""
SSDP_ADDR = "239.255.255.250"
SSDP_PORT = 1900
MS = "M-SEARCH * HTTP/1.1\r\nHOST: {}:{}\r\nMAN: 'ssdp:discover'\r\nMX: 2\r\nST: ssdp:all\r\n\r\n".format(SSDP_ADDR, SSDP_PORT)
def sendMsearch(self):
""" Sending M-SEARCH message
"""
ports = []
for address in self.addresses:
ports.append(reactor.listenUDP(0, self, interface=address))
for port in ports:
for num in range(4):
port.write(Base.MS, (Base.SSDP_ADDR,Base.SSDP_PORT))
reactor.callLater(2.5, self.stopMsearch, port) # MX + a wait margin
def stopMsearch(self, port):
""" Stop listening on port
"""
port.stopListening()
Error:
Traceback (most recent call last):
File "work\find_devices.py", line 56, in sendMsearch
ports.append(reactor.listenUDP(0, self, interface=address))
File "C:\Python27\lib\site-packages\twisted\internet\posixbase.py", line 374, in listenUDP
p.startListening()
File "C:\Python27\lib\site-packages\twisted\internet\udp.py", line 172, in startListening
self._connectToProtocol()
File "C:\Python27\lib\site-packages\twisted\internet\udp.py", line 210, in _connectToProtocol
self.protocol.makeConnection(self)
File "C:\Python27\lib\site-packages\twisted\internet\protocol.py", line 709, in makeConnection
assert self.transport == None
AssertionError
Please tell what's wrong in this code and how to correct this.
Also on linux machines, if no device is found on network then it doesn't go to stopMsearch() why ?
A protocol can only have one transport. The loop:
for address in self.addresses:
ports.append(reactor.listenUDP(0, self, interface=address))
tries to create multiple UDP transports and associate them all with self - a single protocol instance.
This is what the assertion error is telling you. The protocol's transport must be None (ie, it must not have a transport). But on the second iteration through the loop, it already has a transport.
Try using multiple protocol instances instead.
if(re.search("USN:.*MediaRenderer", datagram, flags=re.IGNORECASE)):
deviceXML = re.search("LOCATION:(.*.xml)", datagram, flags=re.IGNORECASE).group(1)
I am getting pytlint error "line too long" for 2nd line
How to correct this ?
Cut your line to meet the length (80 chars usually):
if(re.search("USN:.*MediaRenderer", datagram, flags=re.IGNORECASE)):
deviceXML = re.search("LOCATION:(.*.xml)",
datagram,
flags=re.IGNORECASE).group(1)
Or use your own customization of pylint.