Http client with get request - python

Quick question: need pure python script of simple http client without using libs (only socket library possible).
Main task of this client is connect to server, receive greetings, sends get requests and read responses. Also it's good if this code will be compatible with Cython compiler.

I would recommend you using requests https://github.com/kennethreitz/requests package.
But your question looks like an assignment which shall teach you how is http working on TCP communication level. In such case I would recommend you
learn using http protocol over telnet or netcat
then learn TCP communication by Python and repeat, what you already know by telnet

Related

analogs of socket in python

Can you advise me on the analogs of the socket library on Python? The task is this, I need to write a very simple script with which I could execute remote commands in cmd windows. I know how this can be implemented using the socket library, but I would like to know if there are any other libraries for such a case.
Sockets is a low level mechanism by which two systems can communicate each other. Your OS provides this mechanism, there's no analogs.
Next examples come from the application layer and they work with sockets in their lower communication layers: a socket open by your http server, usually 80 or 443 or a websocket open by your browser to communicate with your server. Or the DNS query that your browser executes when tries to resolve a domain name, also works with sockets between your PC and the DNS server.

Python: send file to a server without third-party libraries?

I have a Python client behind a NAT and a python server with a public IP address. My job is to send a pcap file (the size of a few MB) from the client to a server, as well as a dictionary with some data.
Is there any easy way of doing this without resorting to third-party libraries (e.g. twisted, tornado)?
If not, what's the easiest alternative?
I thought I could send the pcap file through http so that it would be easier to read it on the server side, and perhaps I could do the same with the dictionary by first pickling it. Would it be a good solution?
(I have complete control on the server, where I can install whatever)
Is FTP a usable solution for you?
https://docs.python.org/2/library/ftplib.html
http://effbot.org/librarybook/ftplib.htm
If you can install software on the server, and the server allows HTTP connections, you can write your own simple HTTP server (Python has libraries for doing that). If not, the answer would depend on what services are available on the server.
you can use just the classic sockets with TCP!
You just need to send the file via TCP sockets and receive it in a TCP socket server.
Read it as a file, send it and receive it.
This might give you some tip about sockets: https://docs.python.org/2/library/socket.html
Later I'll post a little script in case you didn't solve your doubt.

how to tunnel any Twisted protocol (say HTTP) inside SSH using Twisted?

I would like to make HTTP requests over SSH tunnels using Twisted. I have seen examples of using Twisted to set up SSH local port forwarding, but that's not what I am after.
Instead, it seems to me it should be possible using Twisted to wrap the HTTP traffic inside SSH tunnel directly - ie. without having to set up Twisted to listen on a local port for forwarding traffic.
I've checked how Twisted Conch command-line script does the local port forwarding, in conch.ssh.forwarding. Should that be somehow integrated with a HTTP client? Or, on the other hand, I've read that SSHChannel supports twisted.internet.interfaces.ITransport interface, so it could be given to Protocols to run them over the secure connection? Then there's the new-ish endpoints API in Twisted: I wonder if an endpoint for tunneling traffic from the ssh server onwards would make sense?
Or something else?
I wonder if an endpoint for tunneling traffic from the ssh server onwards would make sense?
It would make a lot of sense.
There is an endpoint that connects a protocol to the stdio of a command running remotely using Conch - twisted.conch.endpoints.SSHCommandClientEndpoint. And development has started (but stalled, it seems) on an endpoint for connecting a protocol to a remote subsystem (eg sftp) using Conch. An endpoint for connecting to a remote address over a tunneled connection using Conch would make a great addition.
The branch which begins to implement SSHSubsystemClientEndpoint might be a useful thing to look at to get an idea of what is involved in writing this new endpoint. There may also be useful refactorings started in that branch that make it easier to add new endpoints like this (since the branch adds exactly the 2nd conch endpoint and probably had to do some work to make some code from the 1st conch endpoint more easily re-usable).

Websockets for client/server communication in chrome packaged app

I have thoroughly looked at the Chrome packaged app website and sample apps but I couldn't find any example related to websockets implementation in an app. I was wondering if there is any example or sample app that uses Websocket for client/server communication in Chrome app? If not then is there any guide? Is it even possible to use WebSocket? I am using Apache HTTP as my server which is in Python.
I'm assuming you're asking about implementing a WebSocket server, because the browser natively supports the client. (Though if you wanted, you could definitely implement the WebSocket client because you have access to the raw TCP interface.)
The Chrome Apps has published a sample WebSocket chat server that servers HTTP requests to load the chat client and uses WebSockets to send messages between clients.
If you look through the implementation, you see it uses the older chrome.socket API to listen to a TCP socket and respond with the correct WebSocket HTTP headers. It does all the bit manipulation to send and receive frames of data as required by the WebSocket spec.

Websocket library for python

Hi I need a websocket server in python which supports the protocol used in chrome 16(protocol version 13). Tornado and twisted are not working. Websockify works but i can't find any documentation for it. I need minimal setup means lesser imports. Please help me out here thanks in advance.
Maybe you could take a look to pywebsocket, it claims to support protocol version 13 and is designed for :
The pywebsocket project aims to provide a WebSocket standalone server
and a WebSocket extension for Apache HTTP Server, mod_pywebsocket.
Autobahn is another implementation of websockets :
Autobahn WebSockets for Python provides an implementation of the
WebSockets protocol which can be used to build WebSockets clients and
servers
ws4py : Websocket for python :
Python library providing support for the WebSocket protocol defined in
RFC 6455
Here are some examples of implementing a websocket server in Python. Be sure to read and apply the comments on the code of the following examples, because there may be some bugs:
http://popdevelop.com/2010/03/a-minimal-python-websocket-server/ : It has been tested on Chrome, according to the author of the code.
http://mumrah.net/websockets-in-python : At the end of this blog page, the author has included the URL to a Python implementation of a websocket server.
http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/: only works on Chrome, according to the author.
This page contains an implementation of a Python websocket server that can be used through imports:
https://github.com/AdrianGaudebert/python-websocket-server
You should know that the license for using this is MIT. It may only work with Python 3.0.
WebSocket Echo example works on Chrome/16.0.912.63.
It uses txWS a simple library for adding WebSockets server support to your favorite Twisted applications.
If you are still interested in using websockify, there is a simple example of using it to build an echo server included](https://github.com/kanaka/websockify/blob/master/tests/echo.py).
You can run it like this (from a websockify checkout):
./tests/echo.py 8080
The browse to localhost:8080/tests/echo.html. Enter localhost, 8080 for the WebSocket host and port and hit connect. You should see the client sending messages and the server echoing them back (with a "You said: " prefix).

Categories