Why I get a error 200 by websocket with python? - python

Code:
const WebSocket = require('ws');
const shaUtil = require('js-sha256');
String.prototype.hashCode = function() {
return shaUtil(String(this));
};
const server = new WebSocket.Server({port:2909}); // Change to another port if you like.
// Hashes of client keys. Generate them using getHash.js and place them here.
const passes = {
'86cec081a5288000ddb804c24ac70de62a5060e5b4f4068b01a01f9f29dddacc': 0, // Receiver's key.
'c01223ad2ba8cdd184ded788cf6d3469111229cbe6cb3939ce24f471e8f257ca': 1, // Buzzer #1's key.
'5476be1700a54be0572b0066b32b5905986641fa163c5eabd52369eb3a2685cf': 2 // Buzzer #2's key...
};
var receiver = null;
var senders = {1: null, 2: null, 3: null, 4: null}; // You can add more/remove buzzers if you want to.
const lockClients = true; // If it is true, a connected client will not be overriden by another client logging in until it disconnects.
if (lockClients) canConnect = function(id) {
if (id == 0) return receiver === null;
return senders[id] === null;
}
else canConnect = function(id) {
return true;
}
function processBuzzer(client) {
if (receiver == null) return;
receiver.send('BUZZER '+client.buzzerId);
//console.log('Buzzer #'+client.buzzerId+' is activated.');
}
function onDisconnect(id) {
if (id === 0) {
receiver = null;
console.log('Receiver has disconnected.');
} else {
senders[id] = null;
console.log('Sender #' + id + ' has disconnected.');
}
}
server.on('connection', function(client) {
client.sendBuzzer = function() {};
client.on('message', function(message) {
if (message.startsWith('PASSWORD: ')) {
let id = passes[message.substring(10).hashCode()];
if (id !== undefined && canConnect(id)) {
if (client.buzzerId !== undefined) onDisconnect(client.buzzerId);
client.buzzerId = id;
if (id === 0) {
receiver = client;
console.log('Receiver has connected.');
} else {
senders[id] = client;
console.log('Sender #' + id + ' has connected.');
client.sendBuzzer = function() { processBuzzer(this) };
client.send('CONNECTED SUCCESSFULLY');
}
}
}
if (message == 'BUZZER') client.sendBuzzer();
});
client.on('close', function() {
if (client.buzzerId !== undefined) onDisconnect(client.buzzerId);
});
});
console.log('Server is running.');
If i started this with nodeJS with "node server.js", it appears Server is running.....no mistakes.....and now, Iwill connect to this server with the receiver.py (Python-File):
serverAddress = 'ws://192.168.1.50:2909' # The server's address.
clientKey = 'receiver' # The client key corresponding to the receiver's hash.
buzzerSoundFile = 'buzzer.wav' # The name/path of the buzzer sound file.
import asyncio
import websockets
from win32com.client import Dispatch
import winsound
from threading import Thread
app = Dispatch('Powerpoint.Application')
def playBuzzerSound():
global buzzerSoundFile
winsound.PlaySound(buzzerSoundFile, winsound.SND_FILENAME+winsound.SND_ASYNC)
async def mainFunction():
global serverAddress, clientKey
async with websockets.connect(serverAddress) as ws:
await ws.send('PASSWORD: ' + clientKey)
while True:
msg = await ws.recv()
if msg.startswith('BUZZER '):
if app.Run('onBuzzerActivated', int(msg[7:])):
Thread(target=playBuzzerSound).start();
asyncio.get_event_loop().run_until_complete(mainFunction())
I got an error 200 like this:
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 200
What's wrong ??

Related

How to fix the connection problem between unity server and python client?

This is the error rising, could you please tell how shall I solve this issue?
I have unity side which is server and there is a server. There is also python side client, I just try to change the color of the sphere using control commands from python side
matlab/_controlUR_ver2/socket_client.py", line 9, in sendRandomColors
s.connect(('192.168.43.18', 1755))
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
This is the python client:
import socket
import random
from time import sleep
def sendRandomColors():
s = socket.socket()
# connect to the server on local computer
s.connect(('192.168.43.18', 1755))
s.send((
str(random.randint(0,255)) + ","+
str(random.randint(0,255)) + ","+
str(random.randint(0,255)) + ","+
str(random.randint(0,255))).encode())
s.close()
for i in range(100):
sendRandomColors()
sleep(1)
and This is the c# server on unity:
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
public class ChangeColor : MonoBehaviour
{
static Socket listener;
private CancellationTokenSource source;
public ManualResetEvent allDone;
public Renderer objectRenderer;
private Color matColor;
public static readonly int PORT = 1755;
public static readonly int WAITTIME = 1;
ChangeColor()
{
source = new CancellationTokenSource();
allDone = new ManualResetEvent(false);
}
// Start is called before the first frame update
async void Start()
{
objectRenderer = GetComponent<Renderer>();
await Task.Run(() => ListenEvents(source.Token));
}
// Update is called once per frame
void Update()
{
objectRenderer.material.color = matColor;
}
private void ListenEvents(CancellationToken token)
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, PORT);
listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (!token.IsCancellationRequested)
{
allDone.Reset();
print("Waiting for a connection... host :" + ipAddress.MapToIPv4().ToString() + " port : " + PORT);
listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);
while(!token.IsCancellationRequested)
{
if (allDone.WaitOne(WAITTIME))
{
break;
}
}
}
}
catch (Exception e)
{
print(e.ToString());
}
}
void AcceptCallback(IAsyncResult ar)
{
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
allDone.Set();
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
void ReadCallback(IAsyncResult ar)
{
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int read = handler.EndReceive(ar);
if (read > 0)
{
state.colorCode.Append(Encoding.ASCII.GetString(state.buffer, 0, read));
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
}
else
{
if (state.colorCode.Length > 1)
{
string content = state.colorCode.ToString();
print($"Read {content.Length} bytes from socket.\n Data : {content}");
SetColors(content);
}
handler.Close();
}
}
//Set color to the Material
private void SetColors (string data)
{
string[] colors = data.Split(',');
matColor = new Color()
{
r = float.Parse(colors[0]) / 255.0f,
g = float.Parse(colors[1]) / 255.0f,
b = float.Parse(colors[2]) / 255.0f,
a = float.Parse(colors[3]) / 255.0f
};
}
private void OnDestroy()
{
source.Cancel();
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder colorCode = new StringBuilder();
}
}

How to change ROS Package to subscribe to general IMU message instead of custom one?

I have created a ROS package that use custom IMU messages. Instead of that I would like to change the implementation to fit sensor_msgs/msg/imu instead of custom one. So first I found the nodes inside the package that subscribe to that custom IMU message. This is that node.
#!/usr/bin/python3
import numpy as np
from node_registry.decorators import rosnode, register
from driver_ros_msgs.msg import GImu as Imu
from deepx_ros_common.coordinate_converter import quaternion2rpy
_sub_topic = "/backhoe/imu"
_qos = 1
#rosnode.parameter("period", 0.025)
#rosnode
def node_name():
return "test_imu"
#rosnode.inject
def yaw_deg():
return 0
#rosnode.subscribe(Imu, _sub_topic, _qos)
def subscrbe_cb(msg):
orientation = msg.imu.orientation
ypr_rad = quaternion2rpy(
[orientation.w, orientation.x, orientation.y, orientation.z])[0]
rosnode.logger.info(f"Output ypr: {np.rad2deg(ypr_rad)}")
register()
So is it enough just to change the line _sub_topic = "/backhoe/imu" into _sub_topic = "/sensor_msgs/msg/imu" or need something more in this node? Also I have IMU driver cpp code like this
#include <tf2/LinearMath/Quaternion.h>
#include <string>
#include <memory>
#include "vectornav_driver/imu_driver.hpp"
namespace vectornav_driver
{
ImuDriver::ImuDriver(
const std::string node_name, const rclcpp::NodeOptions & options)
: Node(node_name, options),
header_('$'),
delim_("\r\n"),
status_start_(26),
status_end_(30),
yaw_start_(31),
yaw_end_(39),
pitch_start_(40),
pitch_end_(48),
roll_start_(49),
roll_end_(57)
{
}
ImuDriver::~ImuDriver()
{
}
void ImuDriver::init()
{
rclcpp::Parameter frame_id;
rclcpp::Parameter ip;
rclcpp::Parameter port;
auto flag_frame_id = get_parameter_or(
"frame_id", frame_id,
rclcpp::Parameter("frame_id", "default_link"));
if (!flag_frame_id) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get frame_id, setting: %s",
frame_id.as_string().c_str());
}
frame_id_ = frame_id.as_string();
auto flag_ip = get_parameter_or(
"ip", ip,
rclcpp::Parameter("ip", "192.168.255.1"));
if (!flag_ip) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get ip, setting: %s",
ip.as_string().c_str());
}
auto flag_port = get_parameter_or(
"port", port,
rclcpp::Parameter("port", 10003));
if (!flag_port) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get port, setting: %d",
port.as_int());
}
auto hardware_id = ip.as_string() + ":" + std::to_string(port.as_int());
rclcpp::Parameter buffer_limit;
auto flag_buffer_limit = get_parameter_or(
"buffer_limit", buffer_limit,
rclcpp::Parameter("buffer_limit", 143));
if (!flag_buffer_limit) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get buffer_limit, setting: %d",
buffer_limit.as_int());
}
buffer_limit_ = buffer_limit.as_int();
rclcpp::Parameter diagnostics_enable;
auto flag_diag_enable = get_parameter_or(
"diagnostics.enable",
diagnostics_enable,
rclcpp::Parameter("diagnostics.enable", false));
if (!flag_diag_enable) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get diagnostics.enable, setting: %d",
diagnostics_enable.as_bool());
}
rclcpp::Parameter min_freq;
auto flag_min_freq = get_parameter_or(
"diagnostics.min_freq",
min_freq,
rclcpp::Parameter("diagnostics.min_freq", 40.0));
if (!flag_min_freq) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get min_freq, setting: %f",
min_freq.as_double());
}
rclcpp::Parameter max_freq;
auto flag_max_freq = get_parameter_or(
"diagnostics.max_freq",
max_freq,
rclcpp::Parameter("diagnostics.max_freq", 45.0));
if (!flag_max_freq) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get max_freq, setting: %f",
max_freq.as_double());
}
rclcpp::Parameter period;
auto flag_period = get_parameter_or(
"diagnostics.period",
period,
rclcpp::Parameter("diagnostics.period", 1.0));
if (!flag_period) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get period, setting: %f",
period.as_double());
}
if (diagnostics_enable.as_bool()) {
min_freq_ = min_freq.as_double();
max_freq_ = max_freq.as_double();
auto freq_param = diagnostic_updater::FrequencyStatusParam(
&min_freq_,
&max_freq_);
freq_status_ = std::make_shared<
diagnostic_updater::FrequencyStatus>(freq_param);
sensor_status_ = std::make_shared<
SensorStatus>();
updater_ = std::make_unique<diagnostic_updater::Updater>(
this, period.as_double());
updater_->add(*freq_status_);
updater_->add(*sensor_status_);
updater_->setHardwareID(hardware_id);
}
rclcpp::Parameter is_unittest;
auto flag_is_unittest = get_parameter_or(
"is_unittest",
is_unittest,
rclcpp::Parameter("is_unittest", false));
if (!flag_is_unittest) {
RCLCPP_WARN_ONCE(
get_logger(),
"Could not get is_unittest, setting: %d",
is_unittest.as_bool());
}
tcp_ = std::make_unique<driver_common_utils::TcpIO>(
ip.as_string(), port.as_int());
if (is_unittest.as_bool()) {
rclcpp::Rate rate(5.0);
rate.sleep();
}
auto flag_init = tcp_->init();
if (flag_init) {
rclcpp::QoS qos(rclcpp::KeepLast(1));
pub_imu_ = create_publisher<GImu>("imu", qos);
boost::asio::async_read_until(
tcp_->getSocket(),
boost::asio::dynamic_buffer(buffer_),
delim_,
boost::bind(
&ImuDriver::receiveHandler,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
tcp_->run();
} else {
RCLCPP_ERROR_ONCE(get_logger(), "Socket init error");
}
}
void ImuDriver::receiveHandler(
const boost::system::error_code & error, std::size_t bytes_transferred)
{
if (rclcpp::ok()) {
if ((!error.failed() || error == boost::asio::error::message_size)) {
if (buffer_.size() > buffer_limit_) {
buffer_.clear();
}
if (bytes_transferred && buffer_.size()) {
freq_status_->tick();
auto buffer = buffer_.substr(0, bytes_transferred);
auto get_header = buffer.at(0);
if (get_header == header_) {
auto status = decode<uint16_t>(
buffer, status_start_, status_end_, true);
auto yaw = toRadian(
decode<float>(
buffer, yaw_start_, yaw_end_));
auto pitch = toRadian(
decode<float>(
buffer, pitch_start_, pitch_end_));
auto roll = toRadian(
decode<float>(
buffer, roll_start_, roll_end_));
tf2::Quaternion quaternion;
quaternion.setRPY(roll, pitch, yaw);
GImu gimu;
gimu.header.frame_id = frame_id_;
gimu.header.stamp = get_clock()->now();
auto mode_bit_1 = boolBitValue(status, 0);
auto mode_bit_2 = boolBitValue(status, 1);
gimu.mode = (mode_bit_1 * 1) + (mode_bit_2 * 2);
gimu.gnss_fix = boolBitValue(status, 2);
gimu.imu_error = boolBitValue(status, 4);
gimu.magnetometer_pressure_error = boolBitValue(status, 5);
gimu.gnss_error = boolBitValue(status, 6);
gimu.gnss_heading_ins = boolBitValue(status, 8);
gimu.gnss_compass = boolBitValue(status, 9);
gimu.imu.orientation.w = quaternion.getW();
gimu.imu.orientation.x = quaternion.getX();
gimu.imu.orientation.y = quaternion.getY();
gimu.imu.orientation.z = quaternion.getZ();
sensor_status_->sensorStatus(
gimu.mode,
gimu.gnss_fix,
gimu.imu_error,
gimu.magnetometer_pressure_error,
gimu.gnss_error);
pub_imu_->publish(gimu);
}
buffer_.erase(0, bytes_transferred);
}
boost::asio::async_read_until(
tcp_->getSocket(),
boost::asio::dynamic_buffer(buffer_),
delim_,
boost::bind(
&ImuDriver::receiveHandler,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} else {
RCLCPP_ERROR(
get_logger(),
"Receive handler error: %s", error.message().c_str());
}
} else {
RCLCPP_INFO(get_logger(), "Receive handler: node shutdown called");
}
}
} // namespace vectornav_driver
so should be done any changes in the driver too?
Thanks

FastApi cannot write cookie in reponse, in docker on vps, how to fix?

Cannot to sign in, i run localy in docker my container, i can sign-in on my machine and docker no error, but on my remote server i cannot to login, it doesn't write cookie in reponse, have no error, just don't write response. It just redirect me on my page which i setted,and after that i got error, cause i have no my cookie authorization key inside cookie.
video
My SignIn method
#auth_router.post('/signin')
async def sign_in(response: Response, username: str = Form(...), password: str = Form(...), recaptchav3: str = Form(...)) -> dict:
is_human = await verify_recaptcha(recaptchav3)
if is_human['success']:
user = await authenticate_user(username, password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail='Invalid username or password',
)
user_obj = await User_Pydantic.from_tortoise_orm(user)
user_token = await generate_token(user_obj)
response.set_cookie(key="Authorization", value=user_token, httponly=True, secure=True, expires=(8*60*60))
response.headers["Authorization"] = user_token
user.jwt_token = user_token
await user.save()
return {
'access_token': user_token,
'token_type': 'bearer'
}
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail='Invalid captcha',
)
How i submit my form js
const request = (method, url, data = null, redirectPage) => {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest()
xhr.open(method, url, true)
// xhr.setRequestHeader('Content-Type', 'multipart/form-data')
xhr.onerror = function (event) {
alert(event)
console.log(event);
};
xhr.onload = () => {
if (xhr.status === 200) {
return window.location.href = redirectPage;
return resolve(JSON.parse(xhr.responseText || '{}'))
} else {
alert(`Request failed with status ${xhr.status}`)
reject(new Error(`Request failed with status ${xhr.status}`))
return window.location.reload();
}
}
if (data) {
if (typeof data === 'string' || data instanceof String || typeof data.constructor == Object){
xhr.send(JSON.stringify(data))
} else {
xhr.send(data)
}
} else {
xhr.send()
}
})
}
signInForm = getElementById('signinform');
handleEvent(signInForm, 'submit', e => {
e.preventDefault();
if(!isEmpty(signInForm)){
signInUsername = getElement('input[name="username"]', signInForm).value;
signInPassword = getElement('input[name="password"]', signInForm).value;
recaptchaV3 = getElement('[name="g-recaptcha-response"]').value;
if(recaptchaV3){
signInData = new FormData();
signInData.append('username', signInUsername);
signInData.append('password', signInPassword);
signInData.append('recaptchav3', recaptchaV3);
isLogened = request('POST', '/signin', signInData, 'dashboard');
} else{
alert('Перезагрузите страницу');
}
}
})
I cannot understand why using the built-in method in fastapi it does not write on the remote server, it worked on the local one, but I solved the problem by writing. token via js.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie(cookieValue) {
var user = getCookie("Authorization");
if (user == "") {
if (cookieValue != "" && cookieValue != null) {
setCookie("Authorization", cookieValue, 365);
}
}
}
signInForm = getElementById('signinform');
handleEvent(signInForm, 'submit', e => {
e.preventDefault();
if(!isEmpty(signInForm)){
signInUsername = getElement('input[name="username"]', signInForm).value;
signInPassword = getElement('input[name="password"]', signInForm).value;
recaptchaV3 = getElement('[name="g-recaptcha-response"]').value;
if(recaptchaV3){
signInData = new FormData();
signInData.append('username', signInUsername);
signInData.append('password', signInPassword);
signInData.append('recaptchav3', recaptchaV3);
isLogened = request('POST', '/signin', signInData);//, 'dashboard');
isLogened.then(result => {
checkCookie(result.access_token);
return window.location.href = 'dashboard';
}, result => {
log('Cant get response')
});
} else{
alert('Перезагрузите страницу');
}
}
})

Mac Chat App, Not getting NSInputStream & NSOutputStream

I am working with a chat application with a simple python localhost server, using NSStream to send and recieve data via network socket connection. App just worked fine in the iPhone application, but not getting stream in the mac application.
My Python Server Code
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
class MacChat(Protocol):
def connectionMade(self):
print "a client connected"
self.factory.clients.append(self)
print "clients are ", self.factory.clients
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
a = data.split(':')
print a
if len(a) > 1:
command = a[0]
content = a[1]
msg = ""
if command == "iam":
self.name = content
msg = self.name + " has joined"
elif command == "msg":
msg = self.name + ": " + content
print msg
for c in self.factory.clients:
c.message(msg)
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.clients = []
factory.protocol = MacChat
reactor.listenTCP(80, factory)
print "Mac Chat server started"
reactor.run()
Mac
ChatViewController.h
#import <Cocoa/Cocoa.h>
#interface ChatViewController : NSViewController
#property (strong,nonatomic) NSString *userName;
#end
ChatViewController.m
#import "ChatViewController.h"
#interface ChatViewController ()<NSTableViewDataSource,NSTableViewDelegate,NSStreamDelegate>
{
NSInputStream *inputStream;
NSOutputStream *outputStream;
NSMutableArray * messages;
}
#property (weak) IBOutlet NSButton *btnSend;
#property (weak) IBOutlet NSTextField *txtMessage;
#property (weak) IBOutlet NSTableView *tableview;
#end
#implementation ChatViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
-(void)setUserName:(NSString *)userName
{
[self initNetworkCommunication];
NSString *response = [NSString stringWithFormat:#"iam:%#",userName];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
messages = [[NSMutableArray alloc] init];
}
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)#"localhost", 80, &readStream, &writeStream);
inputStream = (__bridge_transfer NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
inputStream.delegate=self;
outputStream.delegate=self;
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}
- (IBAction)btnAtnSend:(id)sender {
NSString *response = [NSString stringWithFormat:#"msg:%#", self.txtMessage.stringValue];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
self.txtMessage.stringValue = #"";
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// Get a new ViewCell
NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
// Since this is a single-column table view, this would not be necessary.
// But it's a good practice to do it in order by remember it when a table is multicolumn.
if( [tableColumn.identifier isEqualToString:#"cell"] )
{
NSString *s = (NSString *) [messages objectAtIndex:row];
cellView.textField.stringValue = s;
}
return cellView;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [messages count];
}
-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
return 30;
}
#pragma mark NSStream Delegate
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventOpenCompleted:
NSLog(#"Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (aStream == inputStream) {
uint8_t buffer[1024];
int len;
while ([inputStream hasBytesAvailable]) {
len = (int)[inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output) {
NSLog(#"server said: %#", output);
[self messageReceived:output];
}
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(#"Can not connect to the host!");
break;
case NSStreamEventEndEncountered:
{
[aStream close];
[aStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
break;
default:
NSLog(#"Unknown event");
}
}
- (void) messageReceived:(NSString *)message {
[messages addObject:message];
[self.tableview reloadData];
[self.tableview scrollRowToVisible:messages.count-1];
}
#end
OK. Looks like this is based on the example from raywenderlich.com.
The example is flawed, as mentioned by "glyph" in the comments section:
This tutorial makes some incorrect assumptions about how data is delivered to an application from a network.
Read glyph's post before building on this code.
For your particular problem, you've made a lot of changes to the original code. At a glance, it's not clear to me that "setUserName" is ever even called.
Without that, nothing else will happen.

Python - wifi - reauth ap

Long story short.
Having some problems with some coding in python.
working on a program to spoof mac address and open new trial session on ap,
(this is for education only.)
the program works with ap that has free 1 hour pass.
the program works like this:
shuts off the wifi adapter
runs .bat script, which changes mac address to random mac address.
turns wifi back on.
opens browser and sends login through with new mac address.
repeats every 1 hour.
the problem:
I am able to shut dowm, change address and bring back up. but,
how do i get the new address in a var and pass it through a web browser to re auth myself.
and open a new session of the trial?
import urllib.parse
import urllib.request
import os
import time
def main():
wifidown()
changemac()
wifiup()
reauth()
time.sleep(3600)
def wifidown():
os.system("wmic path win32_networkadapter where index=7 call disable")
def changemac():
os.system("C:\Users\username\somewhere\theprogram\theprogram.bat")
def wifiup():
os.system("wmic path win32_networkadapter where index=7 call enable")
def reauth():
url = 'https://server.name.com/anothername/?client-mac=mymacaddress&ap-mac=addressofap&hash=somehasvaluethatpassesthrough'
values = {'plan' : 'trial',
'zip code' : '12345',
'e mail' : 'anemail#someemail' }
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req)
the_page = response.read()
while True:
main()
Thank you.
Update**
Yes it helps. I was trying to do this way.
macid = "01:02:03:04:05:06"
mac = "?client-mac=" + macid
apid = "01:02:03:04:05:06"
ap = "&ap-mac=" + apid
hashid = "8JTsXjOC7jcCppExAqbsBmFF4kNMePA6wgrMaMI6MLDUO6bZsc8tgQMfEfGY%2Bp9aLEdXfTZCapb%2B93DotAFLlQ%3D%3D"
thehash = "&hash=" + hashid
url = 'https://the.ap.server.website/' + mac + ap + thehash
def main():
wifidown()
changemac()
wifiup()
urlfunction()
time.sleep(3600)
def wifidown():
os.system("wmic path win32_networkadapter where index=7 call disable")
def changemac():
os.system("C:\Users\esc_fn\Desktop\macchanger\macchange.bat")
def urlfunction():
webbrowser.open_new(url)
def wifiup():
os.system("wmic path win32_networkadapter where index=7 call enable")
while True:
main()
the ap uses a script called purchase.js to get the form data and send it.
the code for that is.
var pdate = new Date();
var offerlink = basepath + 'terms/';
$(document).ready(function(){
// Hide Sponsored Access form
$('#complimentary_spn').hide();
$('#xsubad').hide();
$('a#inline').fancybox({
hideOnContentClick: false,
showCloseButton: false,
margin: 0,
padding: 0
});
$('a.iframe').fancybox({
hideOnContentClick: false,
showCloseButton: false,
scrolling: 'no',
margin: 0,
padding: 0,
height: 510
});
$('#triggerComplete').fancybox({
hideOnContentClick: false,
showCloseButton: false,
margin: 0,
padding: 0
});
$('#rateplanid').change(function(){
// Clear all errors
clear_all_errors('#messageBox');
var planid = $(this).val();
if (planid > 0)
{
$('#complimentary_spn').fadeOut('fast');
$('#xsubad').fadeOut('fast');
$('#paid').fadeIn('fast');
// Set offer and restrictions link
$('#offerlink').find('.pop').attr('href', offerlink+'ppu');
}
else
{
$('#paid').fadeOut('fast');
$('#complimentary_spn').fadeIn('fast');
if ($.inArray(planid, do_reg) < 0)
$('#xsubad').fadeIn('fast');
// Set offer and restrictions link
$('#offerlink').find('.pop').attr('href', offerlink+planid);
}
// Set plan cookie to expire in 10 minutes
pdate.setTime(pdate.getTime() + (10 * 60 * 1000));
setCookie('planid', planid, pdate, '/', '', '');
// Reset required fields
set_required_fields();
// Disable submit buttons
check_enable_submit();
$(this).blur();
});
// Set default plan
if (getCookie('planid'))
$('#rateplanid').val(getCookie('planid'));
else if (planid)
$('#rateplanid').val(planid);
// Trigger change to set required fields
$('#rateplanid').trigger('change');
$("#pwreset").click(function(){
$.post(
basepath + 'ajax/resetpw',
{
username: $('#resetuser').val()
},
function(data) {
if (data == '')
{
$.fancybox.close();
return;
}
set_error('resetuser', data);
}
);
});
$('input, select').not('#resetuser').change(function(){
$.post(
actionurl+'/validate',
$('#purchaseForm').serialize() + '&key=' + $(this).attr('name'),
function(data) { validate_done(data) }
);
});
$('input.submitOrderButton, input.startSessionButton').click(function(){
if ($(this).hasClass('opaque'))
return;
$.post(
actionurl+'/validate',
$('#purchaseForm').serialize(),
function(data) { validate_done(data) }
);
});
});
// Override validation error
validate_error = function(json_data)
{
//console.info('purchase.validate_error');
try
{
if (json_data.errors.nobilling)
{
// Pop payment form
$('.iframe').click()
return;
}
$.each(json_data.errors, function(key, msg) {
set_error(key, msg);
});
window.location.hash = '#messageBox';
}
catch (e)
{
console.error('purchase.validate_error - %s - %s', e.name, e.message);
}
};
// Override validation success
validate_success = function(json_data)
{
//console.info('purchase.validate_success');
try
{
var planid = $('#rateplanid').val();
// For Sponsored Access, perform login
if ($.inArray(planid, ['spn']) >= 0)
{
do_login();
return;
}
// For paid access, pop confirmation screen
$('#completePopup').html(json_data.data.pophtml);
$('#triggerComplete').click();
// Track with Omniture
var s = s_gi('comcastwificaptiveportalprod');
s.tl(this,'o','Payment Confirmation Desktop Page');
return;
}
catch (e)
{
console.error('purchase.validate_success - %s - %s', e.name, e.message);
}
};
var confirmed = function()
{
$.fancybox.close();
do_login();
};
var set_required_fields = function()
{
//console.info('purchase.set_required_fields');
// Clear required fields
$('.required').removeClass('required');
var planid = $('#rateplanid').val();
if (planid > 0)
{
// Set required fields
$('input#username, input#password, input#password1, input#password2').addClass('required');
$('input#firstname, input#lastname, input#email').addClass('required');
$('#paymentFormInputs1, #paymentFormInputs2').find(':input').each(function() {
$(this).not('#storeinfo').addClass('required');
});
}
else
{
// Set required fields
$('#complimentary_'+planid).find(':input').each(function() {
$(this).addClass('required');
});
}
};
My question is:
how do I get the new mac address in the variable called macid
how do I get the ap mac address in the variable apid
how do I get the hash it is asking for and put it in the variable hashid
how do I send the appropriate form data through. ie.. plan id.
You just need to auth yourself on the authentication server right? I think process of authentication and re-authentication is the same. Recommend using requests module. Here is link for you.
Try authentication code like this:
>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>
Hope it helps. :)

Categories

Resources