How do i write a simple http web server using twisted framework?
I want a web server that can receive http request and return a response to the client.
Am going through the twisted documentation and am kinda confused (maybe am just lazy), but it does not look very direct on how to do this, especially how does the twisted server receive the request parameters?
This is my effort...
from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
from twisted.protocols import basic
import sys
log.startLogging(sys.stdout)
class InformixProtocol(basic.LineReceiver):
def lineReceived(self, user):
self.transport.write("Hello this is twisted web server!")
self.transport.loseConnection()
class ProxyFactory(http.HTTPFactory):
#protocol = proxy.Proxy
protocol = InformixProtocol
reactor.listenTCP(8080, ProxyFactory())
reactor.run()
Thanks
Gath
Related
I have the following basic tornado app:
import tornado.web
class IndexHandler(tornado.web.RequestHandler):
"""Regular HTTP handler to serve the ping page"""
def get(self):
self.write("OK")
if __name__ == "__main__":
app = tornado.web.Application([
(r"/", IndexHandler),
])
app.listen(8000)
print 'Listening on 0.0.0.0:8000'
tornado.ioloop.IOLoop.instance().start()
This will run on "http://localhost:8000". How would I get this to run and accept connections at ws://localhost:8000?
tornado.web.RequestHandler is used for accepting HTTP requests. For websockets, you need to use tornado.websocket.WebSocketHandler.
Another thing to note is that you can't visit a websocket url directly from the browser. That is, you can't type ws://localhost:8000 in the address bar and expect to connect to the websocket. That is not how websockets work.
A websocket connection is an upgrage connection. Which means, you first have to visit a url via HTTP and then use Javascript to upgrade to websocket.
See an example about how to connect to websocket using Javascript at Mozilla Web Docs.
I have a very simple reverse-proxy script in twisted:
from twisted.internet import reactor
from twisted.web import proxy, server
from twisted.python import log
from twisted.names import client
site = server.Site(proxy.ReverseProxyResource('www.example.com', 80, ''.encode("utf-8")))
reactor.listenTCP(80, site)
reactor.run()
But sometimes when I connect to my server, It redirects to random website like test0.com and sometimes websites that claim I have a virus. I wondering if there is a bug in Twisted? or if anybody has experienced this before?
I have the following code for a portforward proxy. How can I add
ssl support so that the proxy can connect to a server listening with ssl.
here is the code:
from twisted.internet import reactor
from twisted.protocols import portforward
class ProxyServer(portforward.ProxyServer):
def dataReceived(self, data)
portforward.ProxyServer.dataReceived(self, data)
class ProxyFactory(portforward.ProxyFactory):
protocol = ProxyServer
reactor.listenTCP(8080,ProxyFactory("127.0.0.1",443) )
reactor.run()
Check this link out. This excerpt is probably relevant to what you are looking for .
https://twistedmatrix.com/documents/13.2.0/core/howto/ssl.html
with open('server.pem') as keyAndCert:
cert = ssl.PrivateCertificate.loadPEM(keyAndCert.read())
log.startLogging(sys.stdout)
factory = Factory()
factory.protocol = echoserv.Echo
reactor.listenSSL(8000, factory, cert.options())
reactor.run()
"Notice how all of the protocol code from the TCP version of the echo client and server examples is the same (imported or repeated) in these SSL versions - only the reactor method used to initiate a network action is different."
I am trying to learn Twisted, a Python framework, and I want to put a basic application online that, when it receive a message sends it back. I decided to use Heroku to host it, and I followed the instructions on their docs.
import os
from twisted.internet import protocol, reactor
class Echo(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
port = int(os.environ.get('PORT', 5000))
reactor.listenTCP(port, EchoFactory(), interface = '0.0.0.0')
reactor.run()
Its all working except (and I know this is a stupid question), how do I send a message to it? When I am working locally I just do telnet localhost <port>, but now I have no idea.
Also, since heroku connects to a random port how can I know what port it connects my app to?
Thanks.
I'm not very familiar with Twisted, but I'm not sure what you're trying to do is supported on Heroku. Heroku currently only supports HTTP[S] requests and not raw TCP. There are more details in the answers to this question.
If you wanted to connect to your app, you should use the myapp.herokuapp.com host name or any custom domain that you've added.
"Pure Python applications, such as headless processes and evented web frameworks like Twisted, are fully supported on Cedar."
Reference: https://devcenter.heroku.com/articles/python-support
I am currently trying to pull together a basic SSL server in twisted. I pulled the following example right off their website:
from twisted.internet import ssl, reactor
from twisted.internet.protocol import Factory, Protocol
class Echo(Protocol):
def dataReceived(self, data):
"""As soon as any data is received, write it back."""
print "dataReceived: %s" % data
self.transport.write(data)
if __name__ == '__main__':
factory = Factory()
factory.protocol = Echo
print "running reactor"
reactor.listenSSL(8080, factory,
ssl.DefaultOpenSSLContextFactory(
"./test/privatekey.pem", "./test/cacert.pem"))
reactor.run()
I then tried to hit this server using firefox by setting the url to https://localhost:8080 yet I receive no response. I do, however, see the data arriving at the server. Any ideas why I'm not getting a response?
You're not sending an http header back to the browser, and you're not closing the connection
You've implemented an SSL echo server here, not an HTTPS server. Use the openssl s_client command to test it interactively, not firefox (or any other HTTP client, for that matter).